Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

datetime - Rails doesn't generate created_at for fixture

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As explained in https://stackoverflow.com/a/4350202/978525 you can add something like the following to your fixture objects:

objectA:
  foo: something
  created_at: <%= 5.day.ago.to_s(:db) %>
  updated_at: <%= 5.day.ago.to_s(:db) %>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...