#34491 closed Bug (wontfix)

Unable to have constraints with same name on different models

Reported by: Matheus Cansian Owned by: nobody
Component: Core (System checks) Version: 4.2
Severity: Normal Keywords:
Cc: Simon Charette Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

PR 11278 (ticket #30396) added system checks to enforce uniqueness of indexes and constraints names. The issue is that (at least for PostgreSQL) the check is too strict. The namespace for table constraints (such as CHECK, UNIQUE, FK) is the table itself. This means that you could have the same name for a constraint if they are on different tables. Unfortunately, this system check prevents you for doing that, forcing you to add the unnecessary %(app_label)s_%(class)s to every constraint name.

Considering two models (ModelA and ModelB) both with one CheckConstraint with the same name "test_constraint". The system check would return the following error:

?: (models.E032) constraint name 'test_constraint' is not unique among models: app.ModelA, app.ModelB.

https://github.com/django/django/pull/11278

Change History (1)

comment:1 by Mariusz Felisiak, 20 months ago

Cc: Simon Charette added
Resolution: wontfix
Status: newclosed

Thanks for the report. It's true that constraints on different tables may have the same names on PostgreSQL. However, it's not truth on other databases. Moreover, using the same names is a rare practice when we take into account that it has not been noticed for 4 years. I'd prefer to keep the check for cross-database compatibility. You can always silence it by adding SILENCED_SYSTEM_CHECKS = ["models.E032"] to the settings.

I hope that makes sense.

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