Creating Pyramid/SQLAlchemy models from MySQL database

感情迁移 提交于 2019-12-06 14:16:09

问题


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:


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.



来源:https://stackoverflow.com/questions/27361454/creating-pyramid-sqlalchemy-models-from-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!