I'm looking at the foreigner gem and trying to create some foreign keys. However, the gems documentation says that you should create your foreign keys like this `add_foreign_key(from_table, to_table, options) but when I do that, it seems like it works backwards. For example, I have 3 models, Entry, Ingredient, and an association called EntryIngredient. Entry has many Ingredients through EntryIngredient, Ingredient has many Entries through EntryIngredients, and EntryIngredient belongs to both of these. Yet this is the code that works:
class EntryIngredient < ActiveRecord::Base
belongs_to :entry
belongs_to :ingredient
end
class CreateEntryIngredients < ActiveRecord::Migration
def self.up
create_table :entry_ingredients do |t|
t.references :entry
t.references :ingredient
t.integer :quantity
t.string :unit
t.timestamps
end
add_index :entry_ingredients, [:entry_id, :ingredient_id]
add_foreign_key :entry_ingredients, :entries, :dependent => :delete
add_foreign_key :entry_ingredients, :ingredients, :dependent => :delete
end
def self.down
drop_table :entry_ingredients
end
end
By the docs I would think that the foreign key should actually be added like this:
add_foreign_key :entries, :entry_ingredients, :dependent => :delete
add_foreign_key :ingredients, :entry_ingredients, :dependent => :delete
But when I run the migration, that returns me a
Mysql2::Error: Key column 'entry_ingredient_id' doesn't exist in table: ALTER TABLE `entries` ADD CONSTRAINT `entries_entry_ingredient_id_fk` FOREIGN KEY (`entry_ingredient_id`) REFERENCES `entry_ingredient`(id) ON DELETE CASCADE
Can someone explain to me whats happening here? Why am I dyslexic?