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

问题描述 投票:0回答:2

我收到错误:

ActiveRecord::Fixture::FixtureError: table "creatures" has no columns named "false".
我在此模型中没有名为 false 的列。

发生什么事了?

这是我的装置:

  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
ruby-on-rails yaml fixtures
2个回答
2
投票

将 no 放在单引号中解决了问题。

如果我在引发错误之前调用调试器:

[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

看起来

no
被解释为
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}]

no
放在单引号中解决了问题:

  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

0
投票

添加具有相同症状的问题的解决方案:

我还在我的 has_one 和 has_many 关联上看到了这些

table parent has no column named child
错误。为了修复它们,我必须从父夹具中删除关联并仅将其添加到子夹具中。因此,对于“父母有很多孩子”协会:

# parents.yml

parent_one:
  name: Parent1
# children.yml

child_one:
  name: Child1
  parent: parent_one

child_two:
  name: Child2
  parent: parent_one
© www.soinside.com 2019 - 2024. All rights reserved.