问题描述
我想对已经存在的MySQL数据库使用Pyramid和SQLAlchemy.是否可以从MySQL表自动创建模型.我不想用手将它们全部写出来.可以通过从服务器检索表和结构,也可以使用包含所有表的MySQL"Create Table ..."脚本.
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.
预先感谢,莱纳斯(Linus)
Thanks in advance,Linus
推荐答案
在SQLAlchemy中,您可以这样反映数据库:
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()
然后,meta.tables是您的表.顺便说一下,它在这里描述: http://docs.sqlalchemy.org/zh/latest/core/reflection.html
Then, meta.tables are your tables.By the way, it is described here: http://docs.sqlalchemy.org/en/latest/core/reflection.html
要基于数据库表生成代码,请使用诸如 https://pypi.python之类的软件包. org/pypi/sqlacodegen 和 http://turbogears.org/2.0 /docs/main/Utilities/sqlautocode.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.
这篇关于从MySQL数据库创建Pyramid/SQLAlchemy模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!