﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
12421	Foreign Key on Non-Primary Field fails due to lack of Index on Related Field w/ MySQL	anonymous	nobody	"SVN Revision 11924

{{{
#!python
class Relation(models.Model):
    code = models.CharField(max_length=2, db_index=True)
    class Meta:
        db_table = u'relation_codes'
}}}

{{{
#!python
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. "		closed	Database layer (models, ORM)	dev		worksforme		clouserw@…	Accepted	0	0	0	0	0	0
