【问题标题】:Dialect-specific sqlalchemy configuration特定方言的 sqlalchemy 配置
【发布时间】:2014-03-08 20:25:29
【问题描述】:

当使用带有多种方言的 sqlalchemy 时,仅添加特定方言的配置,例如 sqlite's foreign key support 或 postgres 的 server_side_cursors 会产生问题,因为所有其他方言都不会理解配置或事件。例如:

# applying postgres configuration to a sqlite3 database fails
>>> sqlalchemy.create_engine("sqlite3:///test.sqlite3", server_side_cursors=True)
...
TypeError: Invalid argument(s) 'server_side_cursors' sent to create_engine(), using configuration SQLiteDialect_pysqlite/NullPool/Engine.  Please check that the keyword arguments are appropriate for this combination of components.

但是,sqlite 不需要此配置,因为它会自动流式传输结果。同样,postgres 不需要启用外键支持,因为这是默认设置。

如何以不破坏其他方言的方式应用这种特定于方言的配置?是否有一些 sqlalchemy 促进了这种分支?还有比isinstance 测试更好的东西吗?

【问题讨论】:

    标签: sqlalchemy


    【解决方案1】:

    给定一个已创建的engine,应该在engine.dialect.name(此处为sqlitepostgresql)或engine.dialect.driver(例如pysqlitepsycopg2)上进行分支。所以外键支持应该在engine.dialect.name == "sqlite" 上分支,因为它适用于所有驱动程序,但server_side_cursors 设置应该在engine.dialect.driver == "psycopg2" 上分支,因为 Postgres 的其他驱动程序不支持此设置。

    感谢 Freenode#sqlalchemy 上的 nosklo 提供指针。

    【讨论】:

      猜你喜欢
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 2021-04-02
      • 2021-05-13
      • 2015-08-05
      相关资源
      最近更新 更多