Pyramid 相当于 Django 的syncdb 命令?
我在 Pyramid + SQLAlchemy + URL Dispatch Wiki Tutorial 中注意到数据库是应用程序运行时在主函数中初始化。
def main(global_config, **settings):
""" This function returns a WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
initialize_sql(engine)
# -- and so on ---
其中 initialize_sql
定义如下:
def initialize_sql(engine):
DBSession.configure(bind=engine)
Base.metadata.bind = engine
Base.metadata.create_all(engine)
try:
session = DBSession()
page = Page('FrontPage', 'initial data')
session.add(page)
transaction.commit()
except IntegrityError:
# already created
pass
它实质上创建了所有表(如果它们不存在)并用一些初始值填充它。很容易理解,但是...
这只是一个演示小型应用程序的教程,因此在生产中通常如何完成它可能会有所不同(或没有...)。这让我想到了我的问题:
当将 Pyramid 与 SQLAlchemy 一起使用时,以这种方式初始化数据库是生产中的典型模式,还是通常使用相当于 managesyncdb
命令的命令手动调用的Django?
I noticed in the Pyramid + SQLAlchemy + URL Dispatch Wiki Tutorial that the database is initialized in the main function when the application is run.
def main(global_config, **settings):
""" This function returns a WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
initialize_sql(engine)
# -- and so on ---
where initialize_sql
is defined as follows:
def initialize_sql(engine):
DBSession.configure(bind=engine)
Base.metadata.bind = engine
Base.metadata.create_all(engine)
try:
session = DBSession()
page = Page('FrontPage', 'initial data')
session.add(page)
transaction.commit()
except IntegrityError:
# already created
pass
which essentially creates all of the tables (if they don't exist) and populates it with some initial values. Easy enough to understand, BUT...
This is just a tutorial to demonstrate a small application, so how it is typically done in production may differ (or not...). This brings me to my question:
When using Pyramid with SQLAlchemy, is it a typical pattern in production for a database to be initialized this way, or is it typical to use something equivalent to a manage syncdb
command in Django that is invoked manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 Pyramid 不对数据模型做出任何假设,因此它不会尝试为您管理它们。这完全取决于您以及您正在使用的特定数据层。
关于使用 SQLAlchemy,可以使用 SQLAlchemy-migrate 包来管理迁移。当您进行设置时,它会为您提供一个
管理
命令来执行迁移。Since Pyramid does not make any assumptions about data models, it does not attempt to manage them for you. This is entirely up to you and what specific data layer you are using.
With respect to using SQLAlchemy, it is possible to manage migrations using the SQLAlchemy-migrate package. When you set this up, it provides you with a
manage
command to perform migrations.