Używanie wielu schematów PostgreSQL z modelami Rails
Mam bazę danych PostgreSQL dla mojej aplikacji Rails. W schemacie o nazwie "public" przechowywane są tabele modeli głównych Rails itd. Stworzyłem schemat "discogs", który będzie miał tabele z nazwami, które są czasami takie same jak w schemacie "public" - co jest jednym z powodów, dla których używam schematów do organizowania tego.
Jak skonfigurować modele ze schematu "discogs" w mojej aplikacji? Będę używał plam słonecznych, aby Solr indeksował również te modele. Nie wiem, jak byś to zrobił. to.
6 answers
PostgreSQL Adapter schema_search_path w bazie danych.yml rozwiązuje twój problem?
development:
adapter: postgresql
encoding: utf-8
database: solidus
host: 127.0.0.1
port: 5432
username: postgres
password: postgres
schema_search_path: "discogs,public"
Lub, można określić różne połączenia dla każdego schematu:
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"
Po każdym zdefiniowanym połączeniu utwórz dwa modele:
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
I, wszystkie modele dziedziczą z odpowiedniego schematu:
class MyModelFromPublic < PublicSchema
set_table_name :my_table_name
end
class MyOtherModelFromDiscoGs < DiscoGsSchema
set_table_name :disco
end
Mam nadzieję, że to pomoże.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-30 14:41:33
Poprawny dla rails 4.2 to:
class Foo < ActiveRecord::Base
self.table_name = 'myschema.foo'
end
Więcej informacji -http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-table_name-3D
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-11 16:53:01
Po prostu zrób
class Foo < ActiveRecord::Base
set_table_name 'myschema.foo'
end
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-15 15:09:43
W migracjach:
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
Opcjonalnie w modelu
class User < ActiveRecord::Base
self.table_name 'settings.users'
end
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-06 18:15:24
Ponieważ {[1] } została usunięta i zastąpiona przez self.table_name
.
Myślę, że powinieneś kodować następująco:
class Foo < ActiveRecord::Base
self.table_name = 'myschema.foo'
end
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-30 07:09:32
Metoda set_table_name
została usunięta. Działa dobrze.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-05 10:07:52