#11702 closed Uncategorized (fixed)
ForeignKey validation should check that to_field is unique
Reported by: | physicsnick | Owned by: | Marcos Moyano |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.1 |
Severity: | Normal | Keywords: | pycamp2010 |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Currently, ForeignKey validation does not check that to_field is unique. Here is an example:
class Author(models.Model): code = models.CharField(max_length=10, db_index=True) first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) class Book(models.Model): title = models.CharField(max_length=100) author = models.ForeignKey(Author, to_field='code')
Django happily accepts these models. For PostgresSQL and Oracle, foreign keys must be unique, but MySQL and SQLite make no such requirement. MySQL requires only that the column be indexed, and SQLite has no requirements (it does not enforce foreign key constraints).
Django requires uniqueness so it should be enforcing this on its own. The documentation of to_field should also specify that it should be unique.
Here is the relevant MySQL documentation:
http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
...MySQL and InnoDB require that the referenced columns be indexed for performance. However, the system does not enforce a requirement that the referenced columns be UNIQUE or be declared NOT NULL.
Here is some extended discussion:
http://groups.google.com/group/django-users/browse_thread/thread/fcd3915a19ae333e
Attachments (2)
Change History (12)
comment:1 by , 15 years ago
milestone: | → 1.2 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 15 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
by , 15 years ago
Attachment: | related.patch added |
---|
comment:3 by , 15 years ago
A humble approach at related field initialization. Don't know how to write a test suite for this since it's on initialization. I've ran the test suite just to make sure nothing breaks.
comment:4 by , 15 years ago
Has patch: | set |
---|---|
Keywords: | pycamp2010 added |
Patch needs improvement: | set |
comment:5 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
New patch. Moved validation code to django.core.management.validation and add test suite.
I think the validation could be clearear and simpler if we add a line like this:
self.to_field = to_field
in django.db.models.fields.related on ForeignKey init method.
comment:6 by , 15 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
It's not fixed until patch is committed.
comment:7 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
comment:9 by , 13 years ago
Easy pickings: | unset |
---|---|
Severity: | → Normal |
Type: | → Uncategorized |
UI/UX: | unset |
Shouldn't this fix be specific based on the db being used? i.e syncdb should check the current db being used and raise an error only for postgresql or oracle. Am using mysql and i shouldn't be blocked from using a non_unique field as a foreign key.
comment:10 by , 13 years ago
No. The validation is needed especially for databases that don't enforce this constraint. Django's ForeignKey is many-to-one, the Django code relies on that characteristic, and you can get odd results if the database does not enforce it, see for example this thread:
django.db.fields.related patch