【问题标题】:Rails fixtures and namespace model - ActiveRecord::Fixture::FixtureErrorRails 固定装置和命名空间模型 - ActiveRecord::Fixture::FixtureError
【发布时间】:2018-05-07 20:29:26
【问题描述】:

在一个应用程序中,我使用 Minitest 和固定装置完成了测试套件,我遇到了这样一个问题,我无法解决,也找不到解决方案。

我有一个模型Survey 和它的固定装置test/fixtures/surveys.yml

one:
  user: admin
  name: Survey one
  description: This is survey one
  company: university
  review_type: 1

two:
  user: admin
  name: Survey two
  description: This is survey two
  company: university

另外,我有命名空间surveys,还有一个问题模型-Surveys::Question(具有多态关联),它有相应的固定装置test/fixtures/surveys/questions.yml

one:
  sentence: Survey question one?
  display_order: 1
  question_type: 0
  questionable: one (Survey)

two:
  sentence: Survey question two?
  display_order: 2
  question_type: 1
  questionable: one (Survey)

到目前为止一切顺利,一切正常。但是每当我尝试向夹具添加另一个 questions 定义时,假设:

three:
  sentence: Survey question three?
  display_order: 3
  question_type: 2
  questionable: one (Survey)

出现错误:ActiveRecord::Fixture::FixtureError: table "surveys" has no column named "sentence"

我完全追不上,怎么突然ActiveRecord::Fixtures追不上。

如果您对此提出任何建议,我将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby minitest fixtures


    【解决方案1】:

    确保您的数据库架构未更改,并且您未删除或重命名数据库中的列或表。每当您更改架构并运行迁移时,请务必在运行测试之前运行它:

    rake db:test:prepare
    

    然后尝试再次运行您的测试。如果您收到相同的错误,则表明您的测试数据库缺少该列。检查您的迁移以确保。

    另外,您认为test/fixtures/surveys.yml 中的three: 是否需要?

    【讨论】:

    • 这太棒了,正如你所建议的,我在test/fixtures/surveys.yml 中添加了:three:,然后它突然就起作用了。也许,由于多态关联,需要一对一的覆盖。
    猜你喜欢
    • 2023-03-20
    • 2023-02-06
    • 2017-03-07
    • 2011-05-23
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2012-07-08
    • 2017-03-21
    相关资源
    最近更新 更多