I am using SQLAlchemy-Migrate to manage migrations for my PostgreSQL database. I changed the __tablename__
for a model, and running the migration changed the name in the database, but all the rows in the table were deleted. How can I rename a model without deleting data?
class Contract(db.Model):
__tablename__ = 'contract'
id = db.Column(db.Integer, primary_key=True)
is_valid = db.Column(db.Boolean, default=IS_VALID)
I rename it from contract
to contracts
and get this migration:
def upgrade(migrate_engine):
pre_meta.bind = migrate_engine
post_meta.bind = migrate_engine
pre_meta.tables['contract'].drop()
post_meta.tables['contracts'].create()
It drops the old table and creates a new one. I never examined other migration scripts because they always ran without dropping the data.