Rails不会为fixture创建created_at [英] Rails don't generate created_at for fixture
问题描述
到目前为止没有问题。直到我尝试运行一些测试。我有一个错误,当Rails试图加载我的问题的夹具:
ActiveRecord :: StatementInvalid:SQLite3 :: ConstraintException:questions.created_at可能不是NULL:INSERT INTO问题
这是我的装置:
id:1
question_id:MyString
应用程序:MyString
guid:1
状态: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:sent_at
t.integer:status
t.timestamps
end
end
end
任何想法?
我的模型名称是复数形式。将其更改为单数,rails将生成时间戳。
查看rails如何生成时间戳: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L560
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)
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
"
Here's my fixture :
one:
id: 1
question_id: MyString
app: MyString
guid: 1
status: 1
elapsed_time: 1
allowed_time: 1
and my model :
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
Any idea ?
My model's name was in plural form. Change it to singular and rails will generate timestamp.
See how rails generate timestamp : https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L560
这篇关于Rails不会为fixture创建created_at的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!