ruby on rails - Index's not being created -
i trying create user_roles
table in engine joins user particular role allowing user have 1 or more roles.
i have following migrations:
user
-- migration works fine.
class createxaaronusers < activerecord::migration def change create_table :xaaron_users |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
roles
-- migration works fine
class roles < activerecord::migration def change create_table :xaaron_roles |t| t.string :role t.timestamps end end end
user_roles
-- migration explodes stating column user_id doesn't exist. assume migration, dealing indexes , such, create appropriate columns referencing telling reference.
class userrolesjoin < activerecord::migration def change create_table :xaaron_user_roles, id: false |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
the exact error is:
pg::undefinedcolumn: error: column "user_id" 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''
did fail @ typing something? why migration failing, aside obvious?
if want create join table then,
1. remove existing migration
rails d migration userrolesjoin
2. create new migration join table as
rails g migration createjointableuserrole user role
this create migration like:
class createjointableuserrole < activerecord::migration def change create_join_table :users, :roles |t| # t.index [:user_id, :role_id] # t.index [:role_id, :user_id] end end end
note: need uncomment 1 of combination per requirement generated migration.
3. run rake db:migrate
Comments
Post a Comment