1

I would like to use Pyramid and SQLAlchemy with an already existing MySQL database. Is it possible to automatically create the models from the MySQL tables. I do not want to write them all by hand. This could be either by retrieving the tables and structure from the Server or using a MySQL "Create Table..." script, which contains all the tables.

Thanks in advance, Linus

1 Answer 1

2

In SQLAlchemy you can reflect your database like this:

    from sqlalchemy import create_engine, MetaData

    engine = create_engine(uri)
    meta = MetaData(bind=engine)
    meta.reflect()

Then, meta.tables are your tables. By the way, it is described here: http://docs.sqlalchemy.org/en/latest/core/reflection.html

To generate the code based on the database tables there are packages such as https://pypi.python.org/pypi/sqlacodegen and http://turbogears.org/2.0/docs/main/Utilities/sqlautocode.html , but I haven't used them.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! One more question. How do i get from the table object to plain code. So i do not have to reflect my db every single time and work on it in my project?
Merci i'll have a look

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.