I have two migrations as below:
CreateGroups
t.string :name
t.string :groupkey
CreateEvents
t.string :name
t.string :groupkeyname
t.integer :group_id
and their corresponding models:
group
has_many :events
event
belongs_to :group
from what I understand is that the t.integer :group_id column in the CreateEvents migration references the default id column in the CreateGroups migration. However, I want :groupkeyname from CreateEvents to reference :groupkey in CreateGroups such that when I add an event with groupkeyname = groupkey it automatically references the group in question. How do i do that!
You can specify the custom foreign key in your model.
Check out this link : http://paulsturgess.co.uk/articles/32-custom-foreign-keys-in-ruby-on-rails
or if you know the sql command for the foreign key you can write that command in migration file.
Check this link: How to define foreign key, index constraints in my migration script