英文:
Is there an ORM library for Golang that support multiple schemas of Postgresql?
问题
我正在寻找一个适用于Golang的ORM库,可以支持与多个Postgresql模式一起使用。
这个想法是为了支持使用Postgres模式进行多租户(即在每个请求之前,检查应查询哪个租户,并在查询之前动态设置模式)。
有人熟悉这样的库吗?
英文:
I'm looking for an ORM library for Golang that supports working with multiple Postgresql schemas.
The idea is to support multi-tenancy using Postgres schemas. (i.e. upon each request, check which tenant should be queried and dynamically set the schema before querying)
Is anyone familiar with such a library?
答案1
得分: 2
请注意,到目前为止,我只使用了标准库,因此我对ORM与database/sql
包的行为类似的假设可能是错误的。
我相信任何一个暴露类似于标准库的接口的ORM,比如xorm和gorm,应该能够通过设置search_path
来实现在不同模式之间切换。
db.Exec("SET search_path TO tenant_schema_123")
这将为数据库句柄设置模式,使用该数据库句柄进行的任何后续查询都将发送到tenant_schema_123
。
英文:
Please note that I've only used the standard library so far and therefore my assumption that an ORM will behave in the same manner as the database/sql
package might very well be wrong.
I believe any ORM that exposes an interface analogous to the standard one that allows you to execute plain sql statements, like xorm and gorm for example do, should be able to facilitate switching between schemas by setting the search_path
.
db.Exec("SET search_path TO tenant_schema_123")
This sets the schema for the db handle and any successive queries using that db handle are made to tenant_schema_123
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论