Monica Heddneck Monica Heddneck - 6 months ago 14
SQL Question

covert a table definition class from SQLAlchemy into vanilla MySQL

I am trying to read in a database dump, but I must have the schema defined to do so.

Unfortunately (?), the schema was built using SQLAlchemy. The table definitions are implemented as such:

class Customer(Base):
"""Sqlalchemy model."""
__tablename__ = "customer"

id = Column(Integer, primary_key=True, autoincrement=False)

name = Column('name', String(64), nullable=False)
given_name = Column('given_name', String(32), nullable=True)
family_name = Column('family_name', String(32), nullable=True)


etc etc.

Is there any way to easily convert this back into a MySQL table definition? I can't find anything that would do so. I would like to be able to do this in a scaleable manner, and not type it all by hand each time I have to do a database migration. I'm also suspecting that I'm not the first person to ever have tried this.

Answer

From the documentation:

from sqlalchemy.schema import CreateTable
print CreateTable(Customer.__table__).compile(engine)
Comments