How is a unique constraint across three columns defined?

后端 未结 1 463
迷失自我
迷失自我 2020-12-21 20:00

The following EventInvitation model is a simple invitation for one event, sent from a user to another user. I would like to ensure that the invitations are uniq

1条回答
  •  情话喂你
    2020-12-21 20:50

    You need to add the constraint to the table, not the model. To do this using declarative:

    class EventInvitation(db.Model):
        # ...
        __table_args__ = (
            db.UniqueConstraint(event_id, from_id, to_id),
        )
    

    If the table has already been created in the database, you'll need to drop the table and run db.create_all() again, or use Alembic to alter the existing table with a migration.

    0 讨论(0)
提交回复
热议问题