Minitest中的Fixture错误:table没有名为X的列

时间:2016-01-08 12:47:44

标签: ruby-on-rails-4 minitest

我试图为我的user_tests模型设置测试。

运行测试时我得到的错误(很多一个例子):minitest:

ERROR["test_should_get_new", Admin::TestConfigsControllerTest, 2016-01-07 13:04:02 +0100]
test_should_get_new#Admin::TestConfigsControllerTest (1452168242.12s)
ActiveRecord::Fixture::FixtureError:
     ActiveRecord::Fixture::FixtureError: table "user_tests" has no column named "user".

没有名为user的列,但有一个user_id并且在其他灯具中,这是正确的。我的设置有什么问题,对于这个模型,无法找到列?

灯具user_tests.yml

user_test_one:
  started: false
  started_at:
  finished: false
  finished_at:
  invited: true
  invited_at: <%= Time.zone.now %>
  user: michael
  test_config: test_config_one
  test_result:
  test_result_type:

user_test_two:
  started: false
  started_at:
  finished: false
  finished_at:
  test_result:
  test_result_type:
  invited: false
  invited_at:
  user: michael
  test_config: test_config_two
  test_result:
  test_result_type:

此处usertest_configtest_result都应指向user_idtest_config_idtest_result_id列。所有都给出了相同类型的错误。

它适用于users.yml,其中公司和user_group都是指向其他表的链接(请向Michael Hartle致敬,以获得优秀的Ruby on Rails教程):

michael:
  first_name: Michael
  last_name: Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: <%= Time.zone.now %>
  company: my-company
  user_group: my-company-groep

archer:
  first_name: Sterling
  last_name: Archer
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>
  company: my-company
  user_group: my-company-groep

users_test.rb模型的第一行:

class UserTest < ActiveRecord::Base
  belongs_to :user, required: true
  belongs_to :test_config, required: true
  belongs_to :test_result, polymorphic: true
  attr_accessor :step, :send_invitation

无法找到数据库的任何问题。以下是SQLite中usersuser_tests表的屏幕截图:

SQLlite tables

有什么想法吗?

更新:它必须与测试没有找到此灯具的模型user_test.rbuser_tests.yml)。我可以将列usertest_config转换为包含_id但是会​​出现created_atupdated_at列可能不为零的错误({{1 }})。这些应该由Rails处理,但不是。为什么呢?

ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL constraint failed: user_tests.created_at

更新2 :我强烈怀疑问题是由于在模型/表格名称中包含“测试”而造成的

1 个答案:

答案 0 :(得分:2)

将表格和user_test(以及所有变体)中的所有相应代码重命名为user_survey解决了这个问题。