SQLAlchemy:从现有 table 反映的 table 得到不同的 table 排序规则
SQLAlchemy: A table reflected from an existing table gets different table collation
我一直在研究 SqlAlchemy 示例代码 examples.versioned_history.history_meta
: https://docs.sqlalchemy.org/en/14/_modules/examples/versioned_history/history_meta.html.
当我使用它从我现有的 table 创建历史记录 table 时,它们都是使用不正确的 table 排序规则创建的。现有的table都是在Declarative中创建的,如下:
from history_meta import Versioned
class Thingo(Base,Versioned):
__tablename__ = 'thingos'
__table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}
这导致 table 的 table 归类 属性 为 utf_general_ci
(很好!)。当我使用示例 history_meta
代码生成 thingos_history
实例时,这些 table 是使用 latin1_swedish_ci
的 table 排序规则 属性 创建的(糟糕!)。
确保动态生成的 thingos_history
table 获得与源 table 相同的 table 排序规则 属性 的最佳方法是什么?
(如果这在 per-column/per-table 基础上不容易实现,则接受 table 所有 _history tables
使用 UTF8 编码创建)
===========================
按照评论中的建议(感谢@rfkortekaas),我尝试将字符集 (?charset=utf8
) 作为参数添加到 create_engine() 的 URL 查询字符串中以及提供与 connect_args (create_engine(uri, connect_args={'use_unicode':True,'charset':'utf8'})
) 相同的内容,但是当以 history_meta
的方式动态创建 table 时,这是无效的并且似乎被忽略了MYSQL 自己的默认排序规则。
如上所示,生成 thingos_history
的源 thingos
table 使用声明性 table 定义,该定义在 __table_args__
,但这不适用于动态创建的 thingos_history
table。 thingos_history
没有供我提供 __table_args__
的声明性 table class 定义,它是从 thingos
映射器创建的:
table = Table(
local_mapper.local_table.name + "_history",
local_mapper.local_table.metadata,
*cols,
schema=local_mapper.local_table.schema
)
我正在努力解决的问题是 SQLAlchemy 提供了什么语法来设置以这种方式生成的 table 的 table 排序规则,如果没有这方面的规定,有什么选择我必须从 thingos
动态生成一个 thingos_history
table ,其中 做 要么使用源 table 的排序规则 table 或将 table 归类强制为 utf_general_ci
.
如在 docs 中所见,创建 table 的函数接受关键字参数 mysql_charset
。
如果您查看 line 127 上的 examples.versioned_history.history_meta
文件,则会创建历史记录 Table 实例。您可以更改它以包括`mysql_charset='utf8'.
table = Table(local_mapper.local_table.name + "_history",
local_mapper.local_table.metadata,
*cols,
schema=local_mapper.local_table.schema,
mysql_charset='utf8'
)
我一直在研究 SqlAlchemy 示例代码 examples.versioned_history.history_meta
: https://docs.sqlalchemy.org/en/14/_modules/examples/versioned_history/history_meta.html.
当我使用它从我现有的 table 创建历史记录 table 时,它们都是使用不正确的 table 排序规则创建的。现有的table都是在Declarative中创建的,如下:
from history_meta import Versioned
class Thingo(Base,Versioned):
__tablename__ = 'thingos'
__table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}
这导致 table 的 table 归类 属性 为 utf_general_ci
(很好!)。当我使用示例 history_meta
代码生成 thingos_history
实例时,这些 table 是使用 latin1_swedish_ci
的 table 排序规则 属性 创建的(糟糕!)。
确保动态生成的 thingos_history
table 获得与源 table 相同的 table 排序规则 属性 的最佳方法是什么?
(如果这在 per-column/per-table 基础上不容易实现,则接受 table 所有 _history tables
使用 UTF8 编码创建)
===========================
按照评论中的建议(感谢@rfkortekaas),我尝试将字符集 (?charset=utf8
) 作为参数添加到 create_engine() 的 URL 查询字符串中以及提供与 connect_args (create_engine(uri, connect_args={'use_unicode':True,'charset':'utf8'})
) 相同的内容,但是当以 history_meta
的方式动态创建 table 时,这是无效的并且似乎被忽略了MYSQL 自己的默认排序规则。
如上所示,生成 thingos_history
的源 thingos
table 使用声明性 table 定义,该定义在 __table_args__
,但这不适用于动态创建的 thingos_history
table。 thingos_history
没有供我提供 __table_args__
的声明性 table class 定义,它是从 thingos
映射器创建的:
table = Table(
local_mapper.local_table.name + "_history",
local_mapper.local_table.metadata,
*cols,
schema=local_mapper.local_table.schema
)
我正在努力解决的问题是 SQLAlchemy 提供了什么语法来设置以这种方式生成的 table 的 table 排序规则,如果没有这方面的规定,有什么选择我必须从 thingos
动态生成一个 thingos_history
table ,其中 做 要么使用源 table 的排序规则 table 或将 table 归类强制为 utf_general_ci
.
如在 docs 中所见,创建 table 的函数接受关键字参数 mysql_charset
。
如果您查看 line 127 上的 examples.versioned_history.history_meta
文件,则会创建历史记录 Table 实例。您可以更改它以包括`mysql_charset='utf8'.
table = Table(local_mapper.local_table.name + "_history",
local_mapper.local_table.metadata,
*cols,
schema=local_mapper.local_table.schema,
mysql_charset='utf8'
)