0

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

capedCoder
  • 289
  • 1
  • 3
  • 9

1 Answers1

0

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.

Ian Wilson
  • 6,223
  • 1
  • 16
  • 24
  • The column "my column" is not explicitly defined in any of the model classes, I can see it in migrations though, maybe the previous developer might have manually added it in the migration file? – capedCoder Dec 09 '22 at 17:49
  • @capedCoder Do you have direct access to the database? Maybe check and see if the table itself has the correct columns and constraints otherwise you might have all sorts of things out of sync. Ie. with something like the psql command line tool or mysql command line tool. – Ian Wilson Dec 09 '22 at 17:56
  • Yep the table has the correct data, data types and constraints. – capedCoder Dec 09 '22 at 18:34
  • @capedCoder so it has the my_column ? I'm not sure how rows could be added without setting that column to something. – Ian Wilson Dec 09 '22 at 18:44
  • So I actually followed what you said. I saw that the column was written manually in the initial migration file. I assumed that all alembic migration files get generated through the model only. the migration files just convert the model files to sql scripts . I realized like you suggested that we could do an alter column by creating a new migration file. And it worked! – capedCoder Dec 10 '22 at 19:22