Monday, 8 October 2018

Prevent alembic from autogenerating tables

I'm new to so alembic so i might miss a point in its concept but here is the question.

i have some sqlalchemy tables in a flask app like this:

class Data(Base):
__tablename__ = 'Data'
__table_args__ = {'schema': 'schema'}
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)

i initialize my tables:

Base = declarative_base()
engine = create_engine(db_link, pool_size=100, max_overflow=0)
Base.metadata.create_all(engine)
Session = sessionmaker()
Session.configure(bind=engine)

To that point i created the tables in my database manually and everything worked well. To later go productive with my project i want to be able to migrate my database using alembic. Because some tables i'll use (in a different schema) are read-only and created by another program i only want to migrate some of the sqlalchemy tables. Therfore my upgrade script looks like (created by alembic revision --autogenerate):

revision = 'bb1d39b7eee1'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('Data',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    schema='schema'
    )
    ...

when i now use an empty database to migrate my schema into with:

alembic upgrade head

i get the following error:

sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42S01', "[42S01] [M
icrosoft][SQL Server Native Client 11.0][SQL Server]There is already an object n
amed 'Data' in the database. (2714) (SQLExecDirectW)") [SQL: '\nCREATE TABLE schema.
[Data] (\n\tid INTEGER NOT NULL IDENTITY(1,1), \n\tname VARCHAR(max) NOT NULL, \
 \n\tPRIMARY KEY (id), \n\tCHECK (IN (0, 1))\n)\n\n']

It looks like alembic automatically creates all tables and then tries to create those tables in my revision script again. If thats true how can i tell alembic not to create any tables automatically and only run the scripts i create?



from Prevent alembic from autogenerating tables

No comments:

Post a Comment