【发布时间】:2014-04-06 20:13:24
【问题描述】:
我正在尝试在我的引擎中创建一个user_roles 表,该表将用户与特定角色连接起来,从而允许该用户拥有一个或多个角色。
我有以下迁移:
用户
-- 此迁移工作正常。
class CreateXaaronUsers < ActiveRecord::Migration
def change
create_table :xaaron_users do |t|
t.string :first_name
t.string :last_name
t.string :user_name
t.string :email
t.string :password
t.string :salt
t.timestamps
end
end
end
角色
-- 此迁移工作正常
class Roles < ActiveRecord::Migration
def change
create_table :xaaron_roles do |t|
t.string :role
t.timestamps
end
end
end
用户角色
-- 此迁移爆炸性地指出列 user_id 不存在。我假设这种处理索引等的迁移将创建适当的列来引用我告诉它要引用的内容。
class UserRolesJoin < ActiveRecord::Migration
def change
create_table :xaaron_user_roles, id: false do |t|
t.references :xaaron_user, null: false
t.references :xaaron_role, null: false
end
add_index :xaaron_user_roles, :user_id
add_index :xaaron_user_roles, [:role_id, :user_id], unique: true
add_index :xarron_roles, :role, unique: true
end
end
确切的错误是:
PG::UndefinedColumn: ERROR: column "user_id" does not exist
: CREATE INDEX "index_xaaron_user_roles_on_user_id" ON "xaaron_user_roles" ("user_id")/Users/Adam/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `async_exec'
/Users/Adam/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `block in execute''
我是不是打字失败了?除了显而易见的问题,为什么这次迁移会失败?
【问题讨论】:
-
我认为您的联接表中的列“xaaron_user_id”和“xaaron_role_id”而不是“user_id”和“role_id”
标签: ruby-on-rails postgresql migration