简体   繁体   English

使用foreigner gem指定客户的foreignkey列名称

[英]specifying customer foreignkey column name using foreigner gem

I have 2 models, employee and user with a has_one, belongs_to relationship. 我有2个模型,员工和用户具有has_one,belongs_to关系。 The user table has a 'login' column which needs to only be the 'emp_id' value from the employee table. 用户表具有“登录”列,该列仅需为employee表中的“ emp_id”值。 I tried to capture this relationship with the below migration: 我尝试通过以下迁移来捕获这种关系:

      class AddFkToUsers < ActiveRecord::Migration
            def change
            add_foreign_key( :users, :employees, :source_column => :login, :foreign_column 
         => :emp_id )
            end
        end

but when i try to run this migration I get an error msg: 但是当我尝试运行此迁移时,出现错误消息:

    Mysql2::Error: Key column 'employee_id' doesn't exist in table: ALTER TABLE `users` 
    ADD CONSTRAINT `users_employee_id_fk` FOREIGN KEY (`employee_id`) REFERENCES 
   `employees`(id)/home/vasu/.rvm/gems/ruby-1.8.7-p371/gems/activerecord-
    3.2.9/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'

I thought I could specify the foreign column name too, is this not possible? 我以为也可以指定外部列名称,这是不可能的吗?

至少对于外国人版本1.6.0,我认为适合您的情况的正确语法是:

add_foreign_key(:users, :employees, :column => 'login', :primary_key => 'emp_id')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM