How to work with postgres exception restrictions in alembic

Is there a way to create a table with postgresql exception restriction in Alembic without writing an SQL literal?

Consider, for example, this table:

CREATE TABLE reservation ( during tsrange, EXCLUDE USING gist (during WITH &&) ); 

Exception constraints do not seem to be among the available types of constraints in alembic.

Because SQLAlchemy supports ExcludeConstraints

 from sqlalchemy.dialects.postgresql import ExcludeConstraint, TSRANGE class RoomBooking(Base): __tablename__ = 'room_booking' room = Column(Integer(), primary_key=True) during = Column(TSRANGE()) __table_args__ = ( ExcludeConstraint(('room', '='), ('during', '&&')), ) 

but alembic doesn't seem to recognize them, I wonder if there are other ways to reflect such exclusion restrictions in my history of schema changes.

+6
source share
1 answer

Get in the same problem. Solution in alembic:

You need to import the exception constraint at the top of the script:

 from sqlalchemy.dialects.postgresql import ExcludeConstraint op.create_table('mission_event_schedule', sa.Column('id', sa.Integer(), nullable=False), sa.Column('ts_range', postgresql.TSTZRANGE(), nullable=True), sa.PrimaryKeyConstraint('id'), ExcludeConstraint(('ts_range','&&')) ) 
+5
source

Source: https://habr.com/ru/post/982572/


All Articles