How do I use on delete cascade in mysql?

Here’s what you’d include in your components table. CREATE TABLE `components` ( `id` int(10) unsigned NOT NULL auto_increment, `typeId` int(10) unsigned NOT NULL, `moreInfo` VARCHAR(32), — etc PRIMARY KEY (`id`), KEY `type` (`typeId`) CONSTRAINT `myForeignKey` FOREIGN KEY (`typeId`) REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) Just remember that you need to use … Read more

How to use dynamic foreignkey in Django?

Here is how I do it: from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import fields class Photo(models.Model): picture = models.ImageField(null=True, upload_to=’./images/’) caption = models.CharField(_(“Optional caption”),max_length=100,null=True, blank=True) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = fields.GenericForeignKey(‘content_type’, ‘object_id’) class Article(models.Model): …. images = fields.GenericRelation(Photo) You would add something like content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = fields.GenericForeignKey(‘content_type’, … Read more

django admin sort foreign key field list

Sure – you can… ModelAdmin specific ordering via formfield_for_foreignkey Set default global ordering via a Manager Set default ordering via model meta class Form specific ordering then passed to your ModelAdmin ModelAdmin specific method: (the other methods are in my answer in the post linked to above) class MyModelAdmin(admin.ModelAdmin): def formfield_for_foreignkey(self, db_field, request, **kwargs): if … Read more

Problems creating a Foreign-Key relationship on Entity Framework

There is a conflict between your foreign key property in class Dettaglio… public virtual int IDTEST { get; set; } …which has a non-nullable type (int) and therefore cannot be optional and your mapping… this.HasOptional(t => t.TEST_TABLE) //… …where you want the relationship to be optional. If you indeed want an optional relationship use a … Read more

How to list out all Foreign Keys with “WITH NOCHECK” in SQL Server

The following will return the name of the foreign keys in the current database that are disabled i.e. WITH NOCHECK For SQL Server 2005/2008: select * from sys.foreign_keys where is_disabled=1 There was some discussion in the answer about the difference between disabled & not trusted. What’s below explains the differnce Here’s some code to clarify … Read more

ValueError – Cannot assign: must be an instance

You don’t need to pass the department id, the instance itself is enough. The following should work just fine: new_team = Team( nickname = team_name, employee_id = employee_id, department_id = Department.objects.get(password = password, department_name = department_name)) Just a note: don’t ever name your foreign fields something_id. That something is enough. Django is meant to make … Read more

Sqlite / SQLAlchemy: how to enforce Foreign Keys?

For recent versions (SQLAlchemy ~0.7) the SQLAlchemy homepage says: PoolListener is deprecated. Please refer to PoolEvents. Then the example by CarlS becomes: engine = create_engine(database_url) def _fk_pragma_on_connect(dbapi_con, con_record): dbapi_con.execute(‘pragma foreign_keys=ON’) from sqlalchemy import event event.listen(engine, ‘connect’, _fk_pragma_on_connect)

MySQL’s INSERT IGNORE INTO & foreign keys

[NEW ANSWER] Thanks to @NeverEndingQueue for bringing this up. It seems MySQL has finally fixed this issue. I’m not sure which version this problem was first fixed in, but right now I tested with the following version and the problem is not there anymore: mysql> SHOW VARIABLES LIKE “%version%”; +————————-+——————————+ | Variable_name | Value | … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)