Alembic drop and create foreignkey on every migrate without any changes #1351
-
We have a larger project with flask, sqlalchemy and flask-migrate (alembic). Before we used schema, it worked. But now we have to use schema and have the problem. Database: MSSQL It has multibe db (init --multidb) Example: Model class CustomerAddress(db.Model):
"""Model for table customer_address"""
__bind_key__ = "Customer"
__tablename__ = "customer_address"
__table_args__ = {"schema": "dbo"}
_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
customer_id = db.Column(db.ForeignKey("dbo.customer._id"))
street = db.Column(db.String(255))
house_number = db.Column(db.String(10))
department = db.Column(db.String(50))
zip_code = db.Column(db.String(10))
city = db.Column(db.String(255))
country = db.Column(db.String(255))
customer = db.relationship("Customer", foreign_keys=[customer_id]) class Customer(db.Model):
"""customer table in Customer database"""
__bind_key__ = "Customer"
__tablename__ = "customer"
__table_args__ = {"schema": "dbo"}
_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
description = db.Column(db.String(450), unique=True)
company = db.Column(db.String(450))
supplier_number = db.Column(db.String(450))
sap_customer_number = db.Column(db.String(450))
customer_address = db.relationship(
"CustomerAddress",
cascade="save-update, merge, delete, delete-orphan",
overlaps="customer",
) Initial Migrate def upgrade_Customer():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('customer',
sa.Column('_id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('description', sa.String(length=450), nullable=True),
sa.Column('company', sa.String(length=450), nullable=True),
sa.Column('supplier_number', sa.String(length=450), nullable=True),
sa.Column('sap_customer_number', sa.String(length=450), nullable=True),
sa.PrimaryKeyConstraint('_id', name=op.f('pk_customer')),
sa.UniqueConstraint('description'),
schema='dbo'
)
op.create_table('customer_address',
sa.Column('_id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('customer_id', sa.Integer(), nullable=True),
sa.Column('street', sa.String(length=255), nullable=True),
sa.Column('house_number', sa.String(length=10), nullable=True),
sa.Column('department', sa.String(length=50), nullable=True),
sa.Column('zip_code', sa.String(length=10), nullable=True),
sa.Column('city', sa.String(length=255), nullable=True),
sa.Column('country', sa.String(length=255), nullable=True),
sa.ForeignKeyConstraint(['customer_id'], ['dbo.customer._id'], name=op.f('fk_customer_address_customer_id_customer')),
sa.PrimaryKeyConstraint('_id', name=op.f('pk_customer_address')),
schema='dbo'
)
# ### end Alembic commands ###
def downgrade_Customer():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('customer_address', schema='dbo')
op.drop_table('customer', schema='dbo')
# ### end Alembic commands ### second migrate
def upgrade_Customer():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('customer_address', schema=None) as batch_op:
batch_op.drop_constraint('fk_customer_address_customer_id_customer', type_='foreignkey')
batch_op.create_foreign_key(batch_op.f('fk_customer_address_customer_id_customer'), 'customer', ['customer_id'], ['_id'], referent_schema='dbo')
# ### end Alembic commands ###
def downgrade_Customer():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('customer_address', schema=None) as batch_op:
batch_op.drop_constraint(batch_op.f('fk_customer_address_customer_id_customer'), type_='foreignkey')
batch_op.create_foreign_key('fk_customer_address_customer_id_customer', 'customer', ['customer_id'], ['_id']) Can anyone help? Kind regards |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hi, I believe the issue is that you are using dbo in the foreign keys, and also in the table. |
Beta Was this translation helpful? Give feedback.
ok, looked better at the error and the the impl, not too familiar with mssql.
sqlalchemy default schema for mssql is
dbo
and that's interfering with the reflection of the columns.the solution is to not set the schema in the tables if it's dbo. Only set the schema if a table is in another one