I'm using Rails 5.0 with Postgresql 9.5
I'm in need to add composite primary keys to my model 'User_achievement' (to link 'User' and 'Achievement' models as you may guess).
So I tried using the "composite_primary_keys" gem. I followed all the instructions, nevertheless, the result wasn't like I expected. Seems like it doesn't create pkey in the 'user_achievement' table according to the info by psql tool:
test1_development=> \d user_achievements
Table "public.user_achievements"
Column | Type | Modifiers
----------------+---------+-----------
user_id | integer |
achievement_id | integer |
uach_date | date |
Indexes:
"index_user_achievements_on_achievement_id" btree (achievement_id)
"index_user_achievements_on_user_id" btree (user_id)
Foreign-key constraints:
"fk_rails_4efde02858" FOREIGN KEY (user_id) REFERENCES users(id)
"fk_rails_c44f5b3b25" FOREIGN KEY (achievement_id) REFERENCES achievements(id)
Here's models and migrations' code:
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :name
end
end
end
class CreateAchievements < ActiveRecord::Migration[5.0]
def change
create_table :achievements do |t|
t.string :ach_name
t.text :ach_desc
end
end
end
class CreateUserAchievements < ActiveRecord::Migration[5.0]
def change
create_table :user_achievements, id: false do |t|
t.belongs_to :user, :foreign_key => [:id]
t.belongs_to :achievement, :foreign_key => [:id]
t.date :uach_date
end
end
end
class Achievement < ApplicationRecord
has_many :user_achievements
end
class User < ApplicationRecord
has_many :user_achievements
end
class UserAchievement < ApplicationRecord
self.primary_keys = :user_id, :achievement_id
belongs_to :user, :foreign_key => [:id]
belongs_to :achievement, :foreign_key => [:id]
end
So should the gem alter db tables? or it influences just the rails' environment? Is there the only way to alter db - to add execute line in migration?