Rails Association和Foreign_Key Confusion,我的外键应该是什么?

时间:2012-03-05 19:03:42

标签: mysql ruby-on-rails ruby-on-rails-3 model-associations

我似乎无法理解这种关联应该如何看待和运作。我知道我没有像命名约定那样的轨道,但是我获取数据的源DB也没有,我没有更改主键的自由,因为它们也与我使用的真实源数据库有关。 ETL加载到我的rails数据库。

这些是我的模特:

class Eclass < ActiveRecord::Base
  has_many :elinks, :foreign_key => :concept_id
  has_many :eproperties, :through => :elinks
end

class Elink < ActiveRecord::Base
  belongs_to :eclass, :foreign_key => :concept_id
  belongs_to :eproperty, :foreign_key => :pconcept_id
end

class Eproperty < ActiveRecord::Base
  has_many :elinks, :foreign_key => :pconcept_id
  has_many :eclasses, :through => :elinks
end

这些是MySQL表模式:

mysql> DESC eclasses;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| class_id     | varchar(255) | YES  |     | NULL    |                |
| concept_id   | varchar(255) | NO   | PRI | 1       |                |
| concept_name | varchar(255) | YES  |     | NULL    |                |
| language_id  | varchar(255) | YES  |     | NULL    |                |
| created_at   | datetime     | YES  |     | NULL    |                |
| updated_at   | datetime     | YES  |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

mysql> DESC elinks;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| schema_id   | varchar(255) | YES  |     | NULL    |                |
| concept_id  | varchar(255) | YES  |     | NULL    |                |
| pconcept_id | varchar(255) | YES  |     | NULL    |                |
| data_type   | varchar(255) | YES  |     | NULL    |                |
| language_id | varchar(255) | YES  |     | NULL    |                |
| sequence    | int(11)      | YES  |     | NULL    |                |
| created_at  | datetime     | YES  |     | NULL    |                |
| updated_at  | datetime     | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
9 rows in set (0.02 sec)

mysql> DESC eproperties;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| id            | int(11)      | NO   | PRI | NULL    | auto_increment |
| property_id   | varchar(255) | YES  |     | NULL    |                |
| pconcept_id   | varchar(255) | NO   | PRI | 1       |                |
| property_name | varchar(255) | YES  |     | NULL    |                |
| language_id   | varchar(255) | YES  |     | NULL    |                |
| created_at    | datetime     | YES  |     | NULL    |                |
| updated_at    | datetime     | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

这是我得到的错误,看起来活动记录正在执行一个空列名称的查询:

irb(main):002:0> @eclass.eproperties
  ←[1m←[35mEproperty Load (0.0ms)←[0m  SELECT `eproperties`.* FROM `eproperties`
 INNER JOIN `elinks` ON `eproperties`.`` = `elinks`.`pconcept_id` WHERE `elinks`
.`eclass_id` IS NULL
Mysql2::Error: Unknown column 'elinks.eclass_id' in 'where clause': SELECT `epro
perties`.* FROM `eproperties` INNER JOIN `elinks` ON `eproperties`.`` = `elinks`.`pconcept_id` WHERE `elinks`.`eclass_id` IS NULL
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'elinks.eclass_id'
 in 'where clause': SELECT `eproperties`.* FROM `eproperties` INNER JOIN `elinks` ON `eproperties`.`` = `elinks`.`pconcept_id` WHERE `elinks`.`eclass_id` IS NULL

我想返回给定类的属性。

更新:我在每个答案中添加了外键声明。但仍然收到类似的错误。这是新错误。

irb(main):001:0> @eclass = Eclass.first
  ←[1m←[36mEclass Load (140.6ms)←[0m  ←[1mSELECT `eclasses`.* FROM `eclasses` LIMIT 1←[0m
=> #<Eclass id: 1, class_id: "0161-1#TM-005740#1", concept_id: "0161-1#01-004609#1", concept_name: "ACTUATOR ASSEMBLY,STEERING COLUMN", language_id:
 "0161-1#LG-000001#1", created_at: "2009-06-08 20:28:00", updated_at: "2009-02-03 08:14:00">
irb(main):002:0> @props = @eclass.eproperties
  ←[1m←[35mEproperty Load (0.0ms)←[0m  SELECT `eproperties`.* FROM `eproperties` INNER JOIN `elinks` ON `eproperties`.`` = `elinks`.`pconcept_id` WH
ERE `elinks`.`concept_id` IS NULL
Mysql2::Error: Unknown column 'eproperties.' in 'on clause': SELECT `eproperties`.* FROM `eproperties` INNER JOIN `elinks` ON `eproperties`.`` = `el
inks`.`pconcept_id` WHERE `elinks`.`concept_id` IS NULL
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'eproperties.' in 'on clause': SELECT `eproperties`.* FROM `eproperties` INNER JOIN `e
links` ON `eproperties`.`` = `elinks`.`pconcept_id` WHERE `elinks`.`concept_id` IS NULL
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `query'
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `block in exec_qu
ery'
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
ect_all'
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r

        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
et'
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
        from c:in `load_target'
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/comman
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/comman
        from c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/comman
        from script/rails:6:in `require'

2 个答案:

答案 0 :(得分:2)

我想你会想为每个表设置正确的主键,所以我觉得它看起来像这样。

class Eclass < ActiveRecord::Base
  set_primary_key :concept_id
  has_many :elinks, :foreign_key => :concept_id
  has_many :eproperties, :through => :elinks
end

class Elink < ActiveRecord::Base
  belongs_to :eclass, :foreign_key => :concept_id, :primary_key => :concept_id
  belongs_to :eproperty, :foreign_key => :pconcept_id, :primary_key => :pconcept_id
end

class Eproperty < ActiveRecord::Base
  set_primary_key :pconcept_id
  has_many :elinks, :foreign_key => :pconcept_id
  has_many :eclasses, :through => :elinks
end

答案 1 :(得分:0)

在Elinks类中,我认为你需要在关系中添加外键:

has_many :elinks, :foreign_key => :concept_id