Upate all uniqueness validation to employ UniqueConstraint classes #6982

Closed
opened 2025-12-29 19:47:27 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Sep 14, 2022).

Originally assigned to: @jeremystretch on GitHub.

Proposed Changes

Replace all instances of unique_together under a model Meta class with a list of UniqueConstraint instances defined under constraints. Additionally, and custom overrides of a model's validate_unique() method should be removed where possible.

Justification

UniqueConstraint was introduced in Django 4.0 and allows for conditional evaluation. This should allow us to ditch any custom validate_unique() logic, such as what is currently defined for the Device model. The Django docs also recommend moving away from unique_together in general as it will likely be deprecated in the future.

Originally created by @jeremystretch on GitHub (Sep 14, 2022). Originally assigned to: @jeremystretch on GitHub. ### Proposed Changes Replace all instances of `unique_together` under a model `Meta` class with a list of [`UniqueConstraint`](https://docs.djangoproject.com/en/4.1/ref/models/constraints/#uniqueconstraint) instances defined under `constraints`. Additionally, and custom overrides of a model's `validate_unique()` method should be removed where possible. ### Justification `UniqueConstraint` was introduced in Django 4.0 and allows for conditional evaluation. This should allow us to ditch any custom `validate_unique()` logic, such as what is currently defined for the Device model. The [Django docs](https://docs.djangoproject.com/en/4.1/ref/models/options/#unique-together) also recommend moving away from `unique_together` in general as it will likely be deprecated in the future.
adam added the status: acceptedtype: housekeeping labels 2025-12-29 19:47:27 +01:00
adam closed this issue 2025-12-29 19:47:27 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6982