0

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.

1 Answer 1

1

From the documentation:

from sqlalchemy.schema import CreateTable
print CreateTable(Customer.__table__).compile(engine)
Sign up to request clarification or add additional context in comments.

3 Comments

Looks like it can't handle LONGTEXT type. :( CompileError: (in table 'source', column 'html_source'): Compiler <sqlalchemy.sql.compiler.GenericTypeCompiler object at 0x7f8c1cf97cd0> can't render element of type <class 'sqlalchemy.dialects.mysql.base.LONGTEXT'>
@MonicaHeddneck It should. If you compile with your specific engine it should use a MySQL-specific compiler instead of GenericTypeCompiler, I believe.\
Ah! from sqlalchemy import create_engine mysql_engine = create_engine('mysql://') from sqlalchemy.schema import CreateTable print CreateTable(Customer.__table__).compile(mysql_engine)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.