使用带有Rails模型的多个PostgreSQL模式

时间:2021-12-04 12:51:55

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模型表被存储等等。我创建了一个“迪斯科”模式,它将有一些表,它们的名称有时与“公共”模式相同——这是我使用模式来组织它的原因之一。

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.

如何从应用程序中的“迪斯科”模式设置模型?我将使用太阳黑子让Solr索引这些模型。我不确定你会怎么做。

6 个解决方案

#1


83  

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

数据库中的PostgreSQL适配器schema_search_path。yml能解决你的问题吗?

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.

我希望它有帮助。

#2


8  

Just do

只做

class Foo < ActiveRecord::Base
  set_table_name 'myschema.foo'
end

#3


8  

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

#4


7  

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

#5


7  

Because set_table_name was 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

#6


1  

method set_table_name has been remove. self.table_name is work fine.

方法set_table_name已被删除。自我。table_name是工作好。

#1


83  

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

数据库中的PostgreSQL适配器schema_search_path。yml能解决你的问题吗?

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.

我希望它有帮助。

#2


8  

Just do

只做

class Foo < ActiveRecord::Base
  set_table_name 'myschema.foo'
end

#3


8  

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

#4


7  

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

#5


7  

Because set_table_name was 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

#6


1  

method set_table_name has been remove. self.table_name is work fine.

方法set_table_name已被删除。自我。table_name是工作好。