简体   繁体   English

Rails用错误的外键加载灯具

[英]Rails loading fixtures with wrong foreign keys

I'm writing a simple app using rails 4.1.0. 我正在使用rails 4.1.0编写一个简单的应用程序。 I created two models called Serie and Event, with a one to many relationship between them (as in one Serie has many Events). 我创建了两个名为Serie和Event的模型,它们之间有一对多的关系(因为在一个Serie中有许多事件)。

class Serie < ActiveRecord::Base
  has_many :events
end

class Event < ActiveRecord::Base
  belongs_to :serie
end

I created the following fixture files for these models: 我为这些模型创建了以下fixture文件:

#series.yml
---
Serie1:
  name: Whatever

#events.yml      
---
EventSerie11:
  date: 2013-01-01
  value: '124.4'
  serie: Serie1
EventSerie12:
  date: 2013-02-01
  value: '124.2'
  serie: Serie1

The problem is that when I load these fixtures by using rake db:fixtures:load , I get the wrong foreing keys for the event objects: 问题是,当我使用rake db:fixtures:load加载这些灯具时,我得到了错误的事件对象的外键:

$ rails c
Loading development environment (Rails 4.1.0)
2.1.1 :001 > Serie.first.id
  Serie Load (0.2ms)  SELECT  "series".* FROM "series"   ORDER BY "series"."id" ASC LIMIT 1
 => 10
2.1.1 :002 > Serie.first.events.count
  Serie Load (0.3ms)  SELECT  "series".* FROM "series"   ORDER BY "series"."id" ASC LIMIT 1
   (0.2ms)  SELECT COUNT(*) FROM "events"  WHERE "events"."serie_id" = ?  [["serie_id", 10]]
 => 0  
2.1.1 :003 > Event.first.serie
  Evento Load (0.2ms)  SELECT  "events".* FROM "events"   ORDER BY "events"."id" ASC LIMIT 1
  Serie Load (0.3ms)  SELECT  "series".* FROM "series"  WHERE "series"."id" = ? LIMIT 1  [["id", 627975337]]
 => nil 
2.1.1 :004 > Evento.first.serie_id
  Evento Load (0.4ms)  SELECT  "eventos".* FROM "eventos"   ORDER BY "eventos"."id" ASC LIMIT 1
 => 627975337 

So you see that all fixtures are loading, but the Event records are being assigned an incorrect serie_id foreign key (and since I'm using sqlite for development there are no constraint checks). 所以你看到所有的灯具都在加载,但事件记录被分配了一个不正确的serie_id外键(因为我使用sqlite进行开发,所以没有约束检查)。 As far as I can tell, each time I reset the database and load the fixtures, the Serie record gets a new id, but the serie_id field of each Event record is always set to 627975337. 据我所知,每次重置数据库并加载灯具时,Serie记录都会获得一个新ID,但每个事件记录的serie_id字段始终设置为627975337。

I read here that you can specify the load order for your fixtures; 在这里读到你可以指定灯具的加载顺序; however adding the line 但添加线

ENV["FIXTURES"] ||= "series,events" 

to my environment.rb file didn't work, nor did running 到我的environment.rb文件不起作用,也没有运行

rake db:fixtures:load FIXTURES=series,events

Any ideas? 有任何想法吗?

I think rails is getting in a muddle with your class/fixtures name for series . 我认为rails与你的班级/灯具名称series混淆。 It tries to look for a model class named Series because it struggles to singularize your plural name. 它试图寻找一个名为Series的模型类,因为它很难将你的复数名称单一化。 (Series being a word in english where the plural is the same as the singular). (系列是英语中的单词,其中复数与单数相同)。

I strongly suspect that the Serie instance in your database is nothing to do with your fixture load at all and is instead an instance that has been created via another method. 我强烈怀疑数据库中的Serie实例与您的夹具加载完全无关,而是通过另一种方法创建的实例。

The serie_id used in your events fixtures is correct. 事件装置中使用的serie_id是正确的。 The id that is used is calculated using: 使用的id使用以下公式计算:

> ActiveRecord::FixtureSet.identify(:Serie1)
=> 627975337

which you can see is the same as the value used in your fixtures. 你可以看到它与你的灯具中使用的值相同。

So, a solution, you can either get into the bowels of active record and override the rake task that loads the fixtures - or you can define your singular/plural versions of "serie"/"series" to be what you want rather than what rails is guessing. 所以,一个解决方案,你可以进入活动记录的内容并覆盖加载灯具的rake任务 - 或者你可以定义你的“serie”/“系列”的单数/复数版本,而不是你想要的铁轨正在猜测。 In config/application.rb or an initializer you can add: config/application.rb或初始化程序中,您可以添加:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'serie', 'series'
end

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

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