Django

Code

Ticket #11702 (closed: fixed)

Opened 1 year ago

Last modified 5 months ago

ForeignKey validation should check that to_field is unique

Reported by: physicsnick Assigned to: marcosmoyano
Milestone: 1.2 Component: Database layer (models, ORM)
Version: 1.1 Keywords: pycamp2010
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 1

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

related.patch (1.8 kB) - added by marcosmoyano on 03/06/10 13:55:18.
django.db.fields.related patch
new_patch.patch (2.7 kB) - added by marcosmoyano on 03/06/10 15:48:46.
New patch

Change History

02/03/10 06:59:54 changed by russellm

  • needs_better_patch changed.
  • needs_docs changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • milestone set to 1.2.

03/06/10 10:46:55 changed by marcosmoyano

  • owner changed from nobody to marcosmoyano.
  • status changed from new to assigned.

03/06/10 13:55:18 changed by marcosmoyano

  • attachment related.patch added.

django.db.fields.related patch

03/06/10 14:07:02 changed by marcosmoyano

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.

03/06/10 14:19:24 changed by ramiro

  • keywords set to pycamp2010.
  • needs_better_patch set to 1.
  • has_patch set to 1.

03/06/10 15:48:46 changed by marcosmoyano

  • attachment new_patch.patch added.

New patch

03/06/10 16:06:12 changed by marcosmoyano

  • status changed from assigned to closed.
  • resolution set to fixed.

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.

03/06/10 16:20:52 changed by kmtracey

  • status changed from closed to reopened.
  • resolution deleted.

It's not fixed until patch is committed.

03/10/10 09:44:44 changed by kmtracey

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [12756]) Fixed #11702: Catch to_field specifying a non-unique target in validation. Thanks marcosmoyano.


Add/Change #11702 (ForeignKey validation should check that to_field is unique)




Change Properties
Action