In models.py I have the following model:
class Intro(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
node = models.ForeignKey(Node, on_delete=models.CASCADE)
nav_link = models.ForeignKey(NavLink, on_delete=models.CASCADE)
headline = models.CharField(max_length=48)
class meta:
constraints = [
models.UniqueConstraint(fields=['node', 'nav_link'], name='unique_intro'),
]
The problem is that this UniqueConstraint is being ignored when I add intros. The CREATE TABLE
statement in sqlite doesn’t contain UNIQUE
CREATE TABLE "elements_intro" (
"id" char(32) NOT NULL PRIMARY KEY,
"node_id" char(32) NOT NULL REFERENCES "nodes_node" ("id") DEFERRABLE INITIALLY DEFERRED,
"nav_link_id" char(32) NOT NULL REFERENCES "headers_navlink" ("id") DEFERRABLE INITIALLY DEFERRED,
"headline" varchar(48) NULL,
)
In the Django documentation I found the meta option unique_together
. What’s the difference between the one I used and this one.
Kind regards,
Johanna