#702: document that ManyToMany fields can't be in unique_together
---------------------------------------+----------------------------------
               Reporter:  hugo         |          Owner:  nobody
                   Type:  New feature  |         Status:  new
              Milestone:               |      Component:  Documentation
                Version:  SVN          |       Severity:  Normal
             Resolution:               |       Keywords:  model-validation
           Triage Stage:  Accepted     |      Has patch:  0
    Needs documentation:  0            |    Needs tests:  0
Patch needs improvement:  0            |  Easy pickings:  0
                  UI/UX:  0            |
---------------------------------------+----------------------------------
Changes (by carljm):

 * component:  Database layer (models, ORM) => Documentation


Comment:

 I don't think we should allow m2m fields to be listed in unique_together.

 In practical terms, this is cross-model validation between two models (the
 main model and the m2m through model, which may be implicit). This is
 quite challenging and different from a normal unique_together, because the
 validation code for the main model runs before the m2m models have even
 been saved, so implementing this requires either listening to an
 m2m_changed signal, or putting the validation code onto the m2m through
 model.  In a regular unique_together, there is an actual UNIQUE constraint
 created in the database covering the unique_together fields; with an m2m
 unique-together it could only be enforced by the model-validation code.
 IOW, the underlying implementation would be quite different from the
 implementation of a normal non-m2m unique_together, and those differences
 would be bound to leak up into application code.

 Also, the meaning of listing an m2m field in a unique_together is not
 self-evident, as demonstrated by the IRC conversation in the description
 on this ticket. If "charfield" is unique_together with "m2mfield", does
 that mean charfield must be unique with respect to each individual value
 of m2mfield, or with respect to the combination of m2mfield values?
 Clearly in the `FlatPage` case the former is desired, but I can certainly
 imagine cases in which the latter would be desired.

 Lastly, there's already a sort of precedent here - `ManyToManyFields`
 cannot be set as "unique" (presumably because it would result in similar
 semantic questions and implementation challenges). It seems pretty
 reasonable that a field type which can't be set as unique also can't be
 included in unique_together.

 In light of all this complexity, I think validation of uniqueness related
 to m2m fields should be left up to the application developer, via signals
 or an explicit through model, rather than built in to the ORM. I'm
 changing the component here to documentation, and the summary to reflect
 that we need to document that m2m fields can't be included in
 unique_together.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/702#comment:13>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to