Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#31650 closed Cleanup/optimization (fixed)

Mention uniqueness of constraint names in the "name" attribute docs.

Reported by: Hanne Moa Owned by: Hasan Ramezani
Component: Documentation Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

When using unique_together, Django will create a unique name for you.

What I have seen with inexperienced people using Django 2.2 and newer is that when they use UniqueConstraintinstead of unique_together they build up a name from the fields involved and maybe the word "unique". So, when they have two different models with the same fields needing to be in a unique constraint, they end up with the same name, and hence sooner or later an error.

It should be mentioned in the documentation whenever *Constraint.name is mentioned that the name needs to be unique for the database, and for 3.0 the example should be updated to include %(app_label)s or at a minimum %(class)s.

Change History (5)

comment:1 by Mariusz Felisiak, 4 years ago

Easy pickings: set
Summary: Documentation does not mention that constraint names must be unique per databaseMention uniqueness of constraint names in the "name" attribute docs.
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

It should be mentioned in the documentation whenever *Constraint.name is mentioned that the name needs to be unique for the database ...

It's mentioned in "Constraints in abstract base classes" admonition. Moreover there is a system check for uniqueness of constraints names, see #30396. Nevertheless, I think we can add it also to the CheckConstraint.name and UniqueConstraint.name docs, e.g. "You must always specify a unique name for the constraint.".

... and for 3.0 the example should be updated to include %(app_label)s or at a minimum %(class)s.

I don't agree, you don't need interpolation in most of cases.

comment:2 by Hasan Ramezani, 4 years ago

Has patch: set
Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:3 by GitHub <noreply@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 55556e51:

Fixed #31650 -- Added note uniqueness of constraints names in docs.

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 43254876:

[3.1.x] Fixed #31650 -- Added note uniqueness of constraints names in docs.

Backport of 55556e51fb152c864ddc782d9b56e8b785ccf8bc from master

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In b80c5ba:

[3.0.x] Fixed #31650 -- Added note uniqueness of constraints names in docs.

Backport of 55556e51fb152c864ddc782d9b56e8b785ccf8bc from master

Note: See TracTickets for help on using tickets.
Back to Top