rake db:为拥有架构的角色迁移无效架构错误

时间:2016-08-21 11:39:56

标签: ruby-on-rails postgresql ubuntu

在我的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)

1 个答案:

答案 0 :(得分:0)

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

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

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

2-运行第一次迁移,创建 schema_migrations

文件lib / tasks / 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将对database.yml文件中相应环境的 schema_search_path:&#34; website_dev&#34; 指令中列出的第一个模式执行迁移。