2016-08-21 66 views
0

在我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 db:migrate任务失败并显示错误

ActiveRecord::StatementInvalid: PG::InvalidSchemaName: ERROR: no schema has been selected to create in 
: CREATE TABLE "schema_migrations" ("version" character varying PRIMARY KEY) 
+0

嗨你找到答案了吗?我得到了同样的错误 – BKSpurgeon

+0

是的,我在cloud9.io中遇到了这个问题,因为他们有一组不同的postgres默认值。我已经更新了我的数据库yml,以指向正确的数据库和用户。该临时空间现在已被删除,因此我不能共享〜/ .profile文件 –

回答

0

听起来像Pgsql SEARCH_PATH在第一次迁移之前没有初始化,即使它是在database.yml中定义的。

这里是解决方案,我建议,从我自己的经验:

1-创建rake任务初始化数据库,该数据库创建模式

2-运行其创建第一个迁移在schema_migrations

文件的lib /任务/ 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文件中的相应环境。