SQLAlchemy、反射、不同后端和表/列大小写不敏感
简介:我正在使用支持多个数据库的 SQLAlchemy 反射编写 Web 界面。事实证明,应用程序的作者使用小写的表/列定义了 postgresql,例如。 job.jobstatus,而sqlite则混合大小写,例如Job.JobStatus。我使用示例中的 DeclarativeReflectedBase 来结合反射和声明式风格。
问题:配置 SQLAlchemy 以通过反射处理不区分大小写的表/列
到目前为止我已经完成:
- 我已经更改了DeclarativeReflectedBase.prepare()将 quote=False 传递到 Table.__init__
还有待解决的问题:
- 关系定义在配置连接时仍然必须遵守大小写的方法,例如primaryjoin="Job.JobStatus==Status.JobStatus"。
- 根据引擎类型配置 __tablename__
问题: 我的假设正确还是有更直接的方法? 也许我可以告诉反射反映所有小写字母,所有问题都消失了。
Intro: I'm writing web interface with SQLAlchemy reflection that supports multiple databases. It turns out that authors of application defined postgresql with lowercase tables/columns, eg. job.jobstatus while sqlite has mixed case, eg Job.JobStatus. I'm using DeclarativeReflectedBase from examples to combine reflection and declarative style.
The issue: Configure SQLAlchemy to work with tables/columns case insensitive with reflection
I have done so far:
- I have changed DeclarativeReflectedBase.prepare() method to pass quote=False into Table.__init__
What is left to be solved:
- relationship definitions still has to obey case when configuring joins, like primaryjoin="Job.JobStatus==Status.JobStatus".
- configure __tablename__ based on engine type
The question: Are my assumptions correct or is there more straightforward way? Maybe I could tell reflection to reflect everything lowercase and all problems are gone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想考虑在每个小写的列上定义一个“.key”,这样您就可以在应用程序代码中将列引用为小写。您应该使用column_reflect事件(请参阅http://docs.sqlalchemy .org/en/latest/core/events.html#schema-events)将此键定义为 .name 的小写版本。
然后,当你反映表格时,我会做这样的事情:
我认为这可能会覆盖它。
you'd probably want to look into defining a ".key" on each Column that's in lower case, that way you can refer to columns as lower case within application code. You should use the column_reflect event (See http://docs.sqlalchemy.org/en/latest/core/events.html#schema-events) to define this key as a lower case version of the .name.
then, when you reflect the table, I'd just do something like this:
I think that might cover it.