rails无法索引外键

时间:2014-10-05 15:13:42

标签: ruby-on-rails

我有两个单独的迁移文件:

class CreateLeadProfiles < ActiveRecord::Migration
  def change
    create_table :lead_profiles do |t|
      t.belongs_to :Contact

      t.timestamps
    end
    add_index :lead_profiles, :contact_id
  end
end

class CreateClientProfiles < ActiveRecord::Migration
  def change
    create_table :client_profiles do |t|
      t.belongs_to :Contact
      t.belongs_to :LeadProfile

      t.timestamps
    end
    add_index :client_profiles, :contact_id
    add_index :client_profiles, :lead_profile_id
  end
end

运行rake db:migrate时,出现以下错误:

Mysql2::Error: Key column 'lead_profile_id' doesn't exist in table: CREATE  INDEX `index_client_profiles_on_lead_profile_id`  ON `client_profiles` (`lead_profile_id`)

client_profile belongs_to lead_profile,因此应该有一个lead_profile_id。我希望将该外键编入索引。我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试:

class CreateClientProfiles < ActiveRecord::Migration
  def change
    create_table :client_profiles do |t|
      t.belongs_to :contact
      t.belongs_to :lead_profile

      t.timestamps
    end
    add_index :client_profiles, :contact_id
    add_index :client_profiles, :lead_profile_id
  end
end