ActiveRecord::Fixture::FixtureError: 表格中没有名为 “false” 的列。

huangapple go评论60阅读模式
英文:

ActiveRecord::Fixture::FixtureError: table has no columns named "false"

问题

我遇到了错误:ActiveRecord::Fixture::FixtureError: 表 "creatures" 中没有名为 "false" 的列。 在这个模型中,我没有名为 false 的列。

发生了什么?

这是我的测试数据(fixture):

  3 one:
  4   name: MyString
  5   no: 1
  6   type1: 1
  7   type2: 1
  8   total: 1
  9   hp: 1
 10   attack: 1
 11   defense: 1
 12   special_attack: 1
 13   special_defense: 1
 14   speed: 1
 15   generation: 1
 16   legendary: false
 17
英文:

I am getting the error: ActiveRecord::Fixture::FixtureError: table "creatures" has no columns named "false". I have no column named false in this model.

What is going on?

Here is my fixture:

  3 one:
  4   name: MyString
  5   no: 1
  6   type1: 1
  7   type2: 1
  8   total: 1
  9   hp: 1
 10   attack: 1
 11   defense: 1
 12   special_attack: 1
 13   special_defense: 1
 14   speed: 1
 15   generation: 1
 16   legendary: false
 17

答案1

得分: 2

将单引号括起来解决了问题:

  3 one:
  4   name: MyString
  5   'no': 1
  6   type1: 1
  7   type2: 1
  8   total: 1
  9   hp: 1
 10   attack: 1
 11   defense: 1
 12   special_attack: 1
 13   special_defense: 1
 14   speed: 1
 15   generation: 1
 16   legendary: false
 17 

 18 two:
 19   name: MyString
 20   'no': 2
 21   type1: 1
 22   type2: 1
 23   total: 1
 24   hp: 1
 25   attack: 1
 26   defense: 1
 27   special_attack: 1
 28   special_defense: 1
 29   speed: 1
 30   generation: 1
 31   legendary: false
英文:

Putting the no in single quotes solved the problem.

If I put a debugger call just before the error is raised:

[475, 484] in /usr/local/bundle/gems/activerecord-7.0.4.2/lib/active_record/connection_adapters/abstract/database_statements.rb
   475:             fixture = fixture.stringify_keys
   476: 
   477:             unknown_columns = fixture.keys - columns.keys
   478:             if unknown_columns.any?
   479:               debugger
=> 480:               raise Fixture::FixtureError, %(table "#{table_name}" has no columns named #{unknown_columns.map(&:inspect).join(', ')}.)
   481:             end
   482: 
   483:             columns.map do |name, column|
   484:               if fixture.key?(name)
(byebug) fixtures

It looks like the no got interpreted as a false:

[{"name"=>"MyString", false=>1, "type1"=>1, "type2"=>1, "total"=>1, "hp"=>1, "attack"=>1, "defense"=>1, "special_attack"=>1, "special_defense"=>1, "speed"=>1, "genneration"=>1, "legendary"=>false, "created_at"=>2023-02-05 18:53:31.63881045 UTC, "updated_at"=>2023-02-05 18:53:31.63881045 UTC, "id"=>980190962}, {"name"=>"MyString", false=>2, "type1"=>1, "type2"=>1, "total"=>1, "hp"=>1, "attack"=>1, "defense"=>1, "special_attack"=>1, "special_defense"=>1, "speed"=>1, "genneration"=>1, "legendary"=>false, "created_at"=>2023-02-05 18:53:31.63881045 UTC, "updated_at"=>2023-02-05 18:53:31.63881045 UTC, "id"=>298486374}]

Putting the no in single quotes solved the problem:

  3 one:
  4   name: MyString
  5   'no': 1
  6   type1: 1
  7   type2: 1
  8   total: 1
  9   hp: 1
 10   attack: 1
 11   defense: 1
 12   special_attack: 1
 13   special_defense: 1
 14   speed: 1
 15   generation: 1
 16   legendary: false
 17 
 18 two:
 19   name: MyString
 20   'no': 2
 21   type1: 1
 22   type2: 1
 23   total: 1
 24   hp: 1
 25   attack: 1
 26   defense: 1
 27   special_attack: 1
 28   special_defense: 1
 29   speed: 1
 30   generation: 1
 31   legendary: false

huangapple
  • 本文由 发表于 2023年2月6日 03:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定