Rails 不会为夹具生成 created_at [英] Rails doesn't generate created_at for fixture
问题描述
我有一个模型问题,其字段为question_id"、elapsed_time"、allowed_time"和status",还有一个名为 Reporting 的控制器接收包含问题的 JSON 并将其保存.(question_id"与我的任何模型都不相关)
I have a model Question with "question_id", "elapsed_time", "allowed_time" and "status" as its fields and a controller named Reporting that receive JSON containing questions and save it. ("question_id" is not related to any of my models)
到目前为止,没有任何问题.直到我尝试运行一些测试.当 Rails 尝试加载我的问题的夹具时出现错误:ActiveRecord::StatementInvalid: SQLite3::ConstraintException: questions.created_at 可能不是 NULL: INSERT INTO "questions
"
So far there is no problem. Until I try to run some tests. I've an error when Rails is trying to load my Question's fixture :
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: questions.created_at may not be NULL: INSERT INTO "questions
"
这是我的固定装置:
one:
id: 1
question_id: MyString
app: MyString
guid: 1
status: 1
elapsed_time: 1
allowed_time: 1
还有我的模特:
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.string :app
t.integer :guid
t.string :question_id
t.integer :elapsed_time
t.integer :allowed_time
t.datetime :happened_at
t.integer :status
t.timestamps
end
end
end
有什么想法吗?
推荐答案
我的模特名字是复数形式.将其更改为单数,rails 将生成时间戳.
My model's name was in plural form. Change it to singular and rails will generate timestamp.
查看 rails 如何生成时间戳:https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L560
See how rails generate timestamp : https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L560
这篇关于Rails 不会为夹具生成 created_at的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!