在我的database.yml里
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
database: postgres
username: website
development:
<<: *default
host: localhost
password: password
schema_search_path: "website_dev"在postgres数据库中,我以管理员用户身份运行
ubuntu=# CREATE USER website WITH PASSWORD 'password';
CREATE ROLE
ubuntu=# CREATE SCHEMA website_dev AUTHORIZATION website;
CREATE SCHEMA
ubuntu=# CREATE SCHEMA website_test AUTHORIZATION website;
CREATE SCHEMA这意味着用户/角色网站可以在模式website_dev和website_test中创建表,但是rake :migrate任务由于错误而失败。
ActiveRecord::StatementInvalid: PG::InvalidSchemaName: ERROR: no schema has been selected to create in
: CREATE TABLE "schema_migrations" ("version" character varying PRIMARY KEY)发布于 2016-12-08 21:38:24
听起来,Pgsql SEARCH_PATH在第一次迁移之前没有初始化,尽管它是在database.yml中定义的。
以下是我根据自己的经验提出的解决办法:
1-创建用于初始化数据库的rake任务,这将创建模式。
2-运行创建schema_migrations表的第一个迁移
文件库/任务/db.rake
namespace :db do
desc 'Create database schemas before going for the first migration'
task init: ['db:drop','db:create'] do
ActiveRecord::Base.connection.execute("CREATE SCHEMA website_dev AUTHORIZATION website")
ActiveRecord::Base.connection.execute("CREATE SCHEMA website_test AUTHORIZATION website")
puts 'Database initialised'
end
end运行rake db:init将执行任务db:drop、db:create,然后执行块中的指令。运行rake db:migrate之后,在schema_search_path:"website_dev"指令中列出的第一个模式上执行database.yml文件中相应环境的迁移。
https://stackoverflow.com/questions/39063777
复制相似问题