Rails使用错误的外键加载灯具

时间:2022-03-18 06:16:21

I'm writing a simple app using 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).

我正在使用rails 4.1.0编写一个简单的应用程序。我创建了两个名为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). 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外键(因为我使用sqlite进行开发,所以没有约束检查)。据我所知,每次重置数据库并加载灯具时,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?

1 个解决方案

#1


4  

I think rails is getting in a muddle with your class/fixtures name for series. It tries to look for a model class named Series because it struggles to singularize your plural name. (Series being a word in english where the plural is the same as the singular).

我认为rails与你的班级/灯具名称系列混杂在一起。它试图寻找一个名为Series的模型类,因为它很难将你的复数名称单一化。 (系列是英语中的单词,其中复数与单数相同)。

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. The id that is used is calculated using:

事件装置中使用的serie_id是正确的。使用的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. In config/application.rb or an initializer you can add:

所以,一个解决方案,您既可以进入活动记录的内容并覆盖加载灯具的rake任务 - 或者您可以将您的单个/复数版本的“serie”/“系列”定义为您想要的而不是什么铁轨正在猜测。在config / application.rb或初始化程序中,您可以添加:

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

#1


4  

I think rails is getting in a muddle with your class/fixtures name for series. It tries to look for a model class named Series because it struggles to singularize your plural name. (Series being a word in english where the plural is the same as the singular).

我认为rails与你的班级/灯具名称系列混杂在一起。它试图寻找一个名为Series的模型类,因为它很难将你的复数名称单一化。 (系列是英语中的单词,其中复数与单数相同)。

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. The id that is used is calculated using:

事件装置中使用的serie_id是正确的。使用的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. In config/application.rb or an initializer you can add:

所以,一个解决方案,您既可以进入活动记录的内容并覆盖加载灯具的rake任务 - 或者您可以将您的单个/复数版本的“serie”/“系列”定义为您想要的而不是什么铁轨正在猜测。在config / application.rb或初始化程序中,您可以添加:

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