Alembic doesn't work properly with postgres schemas #1410
-
Trying to make migration for model with custom schema in PostgreSQL, but alembic just recreate model every migration Example model: class User(Base):
__tablename__ = 'users'
__table_args__ = {"schema": "users"}
id = Column(Integer, primary_key=True)
username = Column(String(50), unique=True)
email = Column(String(50))
new_field = Column(Integer) Migration 1: def upgrade() -> None:
op.execute("CREATE SCHEMA users") # added by hand
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('username', sa.String(length=50), nullable=True),
sa.Column('email', sa.String(length=50), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('username'),
schema='users'
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('users', schema='users')
# ### end Alembic commands ###
op.execute("DROP SCHEMA users") # added by hand Migration 2: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('username', sa.String(length=50), nullable=True),
sa.Column('email', sa.String(length=50), nullable=True),
sa.Column('new_field', sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('username'),
schema='users'
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('users', schema='users')
# ### end Alembic commands ### Full code example: https://github.com/MoxForever/test_alembic |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
hi - Schemas are supported so here we would need to see what your env.py looks like. the link you provided is a 404. Please illustrate your env.py inline, we dont prefer to browse code repositories as we need to wade through lots of irrelevant details. make sure you are using include_schemas and also make sure your postgresql search path is set correctly. |
Beta Was this translation helpful? Give feedback.
hi -
Schemas are supported so here we would need to see what your env.py looks like.
the link you provided is a 404. Please illustrate your env.py inline, we dont prefer to browse code repositories as we need to wade through lots of irrelevant details. make sure you are using include_schemas and also make sure your postgresql search path is set correctly.