Linus
Linus

Reputation: 725

Creating Pyramid/SQLAlchemy models from MySQL database

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

Upvotes: 1

Views: 1856

Answers (1)

Petr Blahos
Petr Blahos

Reputation: 2433

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.

Upvotes: 2

Related Questions