diff --git a/app/models.py b/app/models.py index 889479d..ef5391f 100644 --- a/app/models.py +++ b/app/models.py @@ -42,8 +42,27 @@ class ImageRecord(db.Model): ) class Tag(db.Model): + __table_args__ = ( + db.Index( + 'tag_character_with_fandom_uniq', 'name', 'fandom_id', + unique=True, + postgresql_where=db.text("kind = 'character' AND fandom_id IS NOT NULL"), + ), + db.Index( + 'tag_character_null_fandom_uniq', 'name', + unique=True, + postgresql_where=db.text("kind = 'character' AND fandom_id IS NULL"), + ), + db.Index( + 'tag_other_kinds_uniq', 'name', 'kind', + unique=True, + postgresql_where=db.text("kind != 'character'"), + ), + db.Index('tag_kind_idx', 'kind'), + ) + id = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String(255), unique=True, nullable=False) + name = db.Column(db.String(255), nullable=False) kind = db.Column(db.String(64), nullable=True) fandom_id = db.Column(db.Integer, db.ForeignKey("tag.id", ondelete="SET NULL"), nullable=True) fandom = db.relationship("Tag", remote_side="Tag.id", foreign_keys=[fandom_id])