gpt4 book ai didi

ruby-on-rails - 在 Rails 模型中使用多个 PostgreSQL 模式

转载 作者:行者123 更新时间:2023-11-29 11:05:33 24 4
gpt4 key购买 nike

我的 Rails 应用程序有一个 PostgreSQL 数据库。在名为“public”的模式中,存储了主要的 Rails 模型表等。我创建了一个“discogs”模式,其中的表的名称有时与“public”模式中的名称相同——这是原因之一我正在使用模式来组织这个。

如何在我的应用程序中设置来自“discogs”模式的模型?我也将使用 Sunspot 让 Solr 为这些模型建立索引。我不确定您将如何执行此操作。

最佳答案

database.yml 中的 PostgreSQL 适配器 schema_search_path 是否解决了您的问题?

development:
adapter: postgresql
encoding: utf-8
database: solidus
host: 127.0.0.1
port: 5432
username: postgres
password: postgres
schema_search_path: "discogs,public"

或者,您可以为每个模式指定不同的连接:

public_schema:
adapter: postgresql
encoding: utf-8
database: solidus
host: 127.0.0.1
port: 5432
username: postgres
password: postgres
schema_search_path: "public"

discogs_schema:
adapter: postgresql
encoding: utf-8
database: solidus
host: 127.0.0.1
port: 5432
username: postgres
password: postgres
schema_search_path: "discogs"

定义好每个连接后,创建两个模型:

class PublicSchema < ActiveRecord::Base
self.abstract_class = true
establish_connection :public_schema
end

class DiscoGsSchema < ActiveRecord::Base
self.abstract_class = true
establish_connection :discogs_schema
end

并且,您所有的模型都继承自各自的模式:

class MyModelFromPublic < PublicSchema
set_table_name :my_table_name
end

class MyOtherModelFromDiscoGs < DiscoGsSchema
set_table_name :disco
end

关于ruby-on-rails - 在 Rails 模型中使用多个 PostgreSQL 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8806284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com