运行时实体上的 sqlalchemy 动态模式 [英] sqlalchemy dynamic schema on entity at runtime

查看:23
本文介绍了运行时实体上的 sqlalchemy 动态模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SQL Alchemy 并且有一些特定于帐户的架构.架构的名称是使用帐户 ID 派生的,因此在我访问应用程序服务或存储库层之前,我没有架构的名称.我想知道是否可以针对在运行时动态设置其架构的实体运行查询?

I'm using SQL Alchemy and have some schema's that are account specific. The name of the schema is derived using the account ID, so I don't have the name of the schema until I hit my application service or repository layer. I'm wondering if it's possible to run a query against an entity that has it's schema dynamically set at runtime?

我知道我需要设置 __table_args__['schema'] 并尝试使用内置的 type() 来做到这一点,但我总是收到以下错误:

I know I need to set the __table_args__['schema'] and have tried doing that using the type() built-in, but I always get the following error:

could not assemble any primary key columns for mapped table

我准备放弃,直接写sql,但我真的很讨厌那样做.知道如何做到这一点吗?我正在使用 SA 0.99,并且我确实映射了一个 PK.

I'm ready to give up and just write straight sql, but I really hate to do that. Any idea how this can be done? I'm using SA 0.99 and I do have a PK mapped.

谢谢

推荐答案

来自 sqlalchemy 1.1,这可以使用 schema_translation_map 轻松完成.

from sqlalchemy 1.1, this can be done easily using using schema_translation_map.

https://docs.sqlalchemy.org/en/11/changelog/migration_11.html#multi-tenancy-schema-translation-for-table-objects

这篇关于运行时实体上的 sqlalchemy 动态模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆