Pyramid 相当于 Django 的syncdb 命令?

发布于 2024-11-25 06:50:16 字数 1047 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小梨窩很甜 2024-12-02 06:50:16

由于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文