python-3.xsqlalchemysqlalchemy-migrate

How to specify data type while writing a Unique constraint using sql alchemy


I have some python code that I am refactoring, I can see that the tables have a column called "my_column" with a data type integer. Does this automatically get created?

Also is there a way I can explicitly set the data type of "my_column" to BigInt?

enter image description here


Solution

  • A UniqueConstraint is separate from the table columns and doesn't have a data type. In your example there should be a separate Column() named "my_column".

    The unique constraint is only created when doing something like Base.metadata.create_all(engine) or via a migration using something like alembic.

    To alter an existing table you could use something like alembic to create a migration that would only be run once to change a column's datatype.