I am starting building an application using SQLAlchemy as the ORM.
Initially i have defined 18 data models which I know I will need. They range in size from 3 columns to 30+ columns.
Following the SQLAlchemy and alembic documentation i can see to create a migration for these models i should make an upgrade function like this:
def upgrade():
op.create_table(
'account',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('name', sa.String(50), nullable=False),
sa.Column('description', sa.Unicode(200)),
)
I can't see any other way to do this other than to write out my ~200 columns again inside op.create_table functions.
Is there a nicer way to do this with less repetition and less potential for human error? I'm thinking I would like to pass my ORM models (i.e. my classes which subclass declarative base) and have op.create_table
take care of this automatically? I know that i'd need to define these classes within my revision (rather than importing from my models file) to protect against future changes breaking the migration, but copying and pasting them in their current state would be vastly preferable to passing each column into op.create_table