Fixed #7042 -- The management validation command nows alerts users to the

presence (and incorrectness) of unique=True on ManyToManyFields. This has never
worked and generates invalid SQL. Now it's raised as an explicit error during
validation. Thanks to clamothe for the patch.

Still needs a docs change to make this clear, but that goes to the docs
refactor branch.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8488 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-08-23 16:56:41 +00:00
parent 0511435a33
commit 943c28a4c6
2 changed files with 15 additions and 5 deletions

View File

@ -110,6 +110,11 @@ def get_validation_errors(outfile, app=None):
# so skip the next section # so skip the next section
if isinstance(f.rel.to, (str, unicode)): if isinstance(f.rel.to, (str, unicode)):
continue continue
# Check that the field is not set to unique. ManyToManyFields do not support unique.
if f.unique:
e.add(opts, "ManyToManyFields cannot be unique. Remove the unique argument on '%s'." % f.name)
if getattr(f.rel, 'through', None) is not None: if getattr(f.rel, 'through', None) is not None:
if hasattr(f.rel, 'through_model'): if hasattr(f.rel, 'through_model'):
from_model, to_model = cls, f.rel.to from_model, to_model = cls, f.rel.to

View File

@ -177,6 +177,10 @@ class AbstractRelationModel(models.Model):
fk1 = models.ForeignKey('AbstractModel') fk1 = models.ForeignKey('AbstractModel')
fk2 = models.ManyToManyField('AbstractModel') fk2 = models.ManyToManyField('AbstractModel')
class UniqueM2M(models.Model):
""" Model to test for unique ManyToManyFields, which are invalid. """
unique_people = models.ManyToManyField( Person, unique=True )
model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute. model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute.
invalid_models.fielderrors: "decimalfield": DecimalFields require a "decimal_places" attribute. invalid_models.fielderrors: "decimalfield": DecimalFields require a "decimal_places" attribute.
invalid_models.fielderrors: "decimalfield": DecimalFields require a "max_digits" attribute. invalid_models.fielderrors: "decimalfield": DecimalFields require a "max_digits" attribute.
@ -271,4 +275,5 @@ invalid_models.personselfrefm2m: Intermediary model RelationshipTripleFK has mor
invalid_models.personselfrefm2mexplicit: Many-to-many fields with intermediate tables cannot be symmetrical. invalid_models.personselfrefm2mexplicit: Many-to-many fields with intermediate tables cannot be symmetrical.
invalid_models.abstractrelationmodel: 'fk1' has a relation with model AbstractModel, which has either not been installed or is abstract. invalid_models.abstractrelationmodel: 'fk1' has a relation with model AbstractModel, which has either not been installed or is abstract.
invalid_models.abstractrelationmodel: 'fk2' has an m2m relation with model AbstractModel, which has either not been installed or is abstract. invalid_models.abstractrelationmodel: 'fk2' has an m2m relation with model AbstractModel, which has either not been installed or is abstract.
invalid_models.uniquem2m: ManyToManyFields cannot be unique. Remove the unique argument on 'unique_people'.
""" """