2016-08-21 13 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文件中的相應環境。