I'm creating a project in RoR and get a error when run "rake db:migrate":
Mysql2::Error: Key column 'item_id' doesn't exist in table
And I don't understand what I'm doing wrong:
This is my items migration
class CreateMerchants < ActiveRecord::Migration
def change
create_table :merchants do |t|
t.string :name, null: false, limit: 100
t.string :address, null: false, limit: 200
t.timestamps
end
end
end
And my purchases migration:
class CreatePurchases < ActiveRecord::Migration
def change
create_table :purchases do |t|
t.references :purchasers, null: false
t.references :items, null: false
t.references :merchants, null: false
t.decimal :purchase_count, null: false, precision: 17, scale: 2
t.timestamps
end
add_index :purchases, :item_id
add_index :purchases, :merchant_id
add_index :purchases, :purchaser_id
end
end
Somebody can show me where I'm wrong?
The issue is that references expects a singular name, also you can create the index in the same line that create the reference so you migration should looks like:
class CreatePurchases < ActiveRecord::Migration
def change
create_table :purchases do |t|
t.references :purchaser, null: false, index: true
t.references :item, null: false, index: true
t.references :merchant, null: false, index: true
t.decimal :purchase_count, null: false, precision: 17, scale: 2
t.timestamps
end
end
end