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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8806284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:35:31  来源:igfitidea点击:

Using multiple PostgreSQL schemas with Rails models

ruby-on-railspostgresqlmodelsdatabase-schemaschema-design

提问by Johan

I have a PostgreSQL database for my Rails application. In the schema named 'public' the main Rails models tables are stored etc. I have created a 'discogs' schema which will have tables with names that are sometimes the same as in the 'public' schema - which is one of the reasons that I'm using schemas to organize this.

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

How would I setup models from the 'discogs' schema in my app? I will be using Sunspot to let Solr index these models as well. I'm unsure of how you would do this.

我将如何从我的应用程序中的“discogs”模式设置模型?我也将使用 Sunspot 让 Solr 索引这些模型。我不确定你会怎么做。

回答by Felsangom

PostgreSQL adapter schema_search_path in database.yml does solve your problem?

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"

Or, you can to specify different connections for each schema:

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

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"

After each connection defined, create two models:

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

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

And, all your models inherit from the respective schema:

而且,您的所有模型都从各自的架构继承:

class MyModelFromPublic < PublicSchema
  set_table_name :my_table_name
end

class MyOtherModelFromDiscoGs < DiscoGsSchema
  set_table_name :disco
end

I hope it helps.

我希望它有帮助。

回答by Lalu

The correct one for rails 4.2 is as:

rails 4.2 的正确做法是:

class Foo < ActiveRecord::Base
  self.table_name = 'myschema.foo'
end

More info -http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-table_name-3D

更多信息 - http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-table_name-3D

回答by maniek

Just do

做就是了

class Foo < ActiveRecord::Base
  self.table_name = 'myschema.foo'
end

回答by Khanh Pham

Because set_table_namewas removed, and it was replaced by self.table_name.

因为set_table_name被删除了,它被替换了self.table_name

I think you should code follow as:

我认为你应该编码如下:

class Foo < ActiveRecord::Base
  self.table_name =  'myschema.foo'
end

回答by Jaime Castrillon

In migrations:

在迁移中:

class CreateUsers < ActiveRecord::Migration
  def up
    execute 'CREATE SCHEMA settings'
    create_table 'settings.users' do |t|
      t.string :username
      t.string :email
      t.string :password

      t.timestamps null: false
    end
  end

  def down
    drop_table 'settings.users'
    execute 'DROP SCHEMA settings'
  end

end

Optional in model

型号可选

class User < ActiveRecord::Base
  self.table_name 'settings.users'
end

回答by zhulinpinyu

method set_table_namehas been remove. self.table_nameworks fine.

方法set_table_name已删除。self.table_name工作正常。