简体   繁体   English

外国人的宝石如何工作?

[英]How does the foreigner gem work?

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. 但是,gem 文档说您应该像这样创建外键,如`add_foreign_key(from_table,to_table,options),但是当我这样做时,它看起来像是向后工作的。 For example, I have 3 models, Entry, Ingredient, and an association called EntryIngredient. 例如,我有3个模型,Entry,Ingredient和一个名为EntryIngredient的关联。 Entry has many Ingredients through EntryIngredient, Ingredient has many Entries through EntryIngredients, and EntryIngredient belongs to both of these. Entry通过EntryIngredient有很多成分,Ingredient通过EntryIngredients有很多条目,EntryIngredient都属于这两个。 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? 为什么我阅读困难?

I've verified this, if someone thinks I'm still not getting it please explain to me. 我已经证实了这一点,如果有人认为我仍然无法理解,请向我解释。 But, unless my English is bad, I believe that the gem is documented incorrectly. 但是,除非我的英语不好,否则我认为该宝石的记载不正确。 The correct syntax is actually: 正确的语法实际上是:

add_foreign_key(to_table, from_table, options)

So with that I would do add_foreign_key :entry_ingredients, :entries, :dependent => :delete This logic says to me, add a foreign key to the table entry_ingredients, from table entries as entry_id, with options... 这样我就可以执行add_foreign_key :entry_ingredients, :entries, :dependent => :delete这个逻辑告诉我,从表条目entry_id中添加外键到表entry_ingredients,并带有选项...

While by the documentations logic I would use this code add_foreign_key :entries, :entry_ingredients, :dependent => :delete which says to me, add a foreign key from entries (as entry_id), to entry_ingredients, with options... However, what in fact happens is we are adding a foreign key to entries, from entry_ingredients (as entry_ingredients_id). 虽然根据文档逻辑,我会使用此代码add_foreign_key :entries, :entry_ingredients, :dependent => :delete ,它对我说,将条目的外键(作为entry_id)添加到entry_ingredients中,并带有选项...但是,但是实际上是在我们向entry_ingredients(作为entry_ingredients_id)添加条目的外键。 This is wrong, and not the intended result. 这是错误的,而不是预期的结果。 Here is the error I receive when following the documentations method to prove it: 这是遵循文档方法进行证明时收到的错误:

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_ingredients`(id) ON DELETE CASCADE

I've reported this as an issue on github so hopefully he fixes the documentation. 我已经在github上将其报告为问题,因此希望他能修复文档。

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

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