Foreign Key on Non-Primary Field fails due to lack of Index on Related Field w/ MySQL
|Reported by:||anonymous||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
SVN Revision 11924
class Relation(models.Model): code = models.CharField(max_length=2, db_index=True) class Meta: db_table = u'relation_codes'
class Source(models.Model): code = models.ForeignKey('Relation', to_field='code') class Meta: db_table = u'source_codes'
When trying to create the database, I get a "_mysql_exceptions.OperationalError (#150)" on a query with the form "ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY ... REFERENCES" for my foreign key relationship, checking the obvious problems I find that my table to which I'm trying to make a relationship with has no index on the field I wish to make a foreign key relationship with. Adding an index allows for the problem query to be executed successfully. I am assuming an order problem with the indices for non-primary (or non-integer) fields being created after foreign keys are added leading to the problem.
Change History (5)
comment:3 Changed 7 years ago by
|Status:||closed → reopened|