3

I'm getting the following error in Rails with PostgreSQL:

ActiveRecord::StatementInvalid:
       PG::NotNullViolation: ERROR:  null value in column "created_at" violates not-null constraint
       DETAIL:  Failing row contains (4, null, null, 2014-05-08 13:27:56.002747, f, null, null, f, f).
       : INSERT INTO "elements" ("updated_at") VALUES ('2014-05-08 13:27:56.002747') RETURNING "id"

I gather it's saying I'm attempting to insert the value of 2014-05-08 13:27:56.002747 into the updated_at column of a row representing one instance of Element. Is that correct?

This should be fine, as the value looks like a valid date/time to me — correct?

I also gather it's saying created_at can't be null but that seems crazy because doesn't every instance automatically get a created_at value equal to the date/time at which it was created?

And what does RETURNING "id" mean?

I'm happy to continue debugging my app myself, but if anyone could unpack this error message for me it would be a great headstart. (Especially as the stack trace is not making it clear where the error is being created.)

4
  • created_at is NOT NULL and DEFAULT NULL (as you wrote in the comment to @denis), thus the INSERT fails since you are trying to create a row with a NULL value for the created_at column. Can you write the Ruby code which generates this error? Commented May 8, 2014 at 13:54
  • Sorry can't find the line yet. Most of the lines in my stacktrace points to Rails internals, and I've got a few callbacks affecting models triggering callbacks. But why do you say it's DEFAULT NULL? Commented May 8, 2014 at 14:11
  • Because from the comment you wrote I guess that in the migration you don't specify the default value for created_at column, so Rails doesn't set it so the default value is set as NULL in the PostgreSQL column definition. Now that I'm thinking about it, something like t.datetime 'created_at', null: false, default: 'NOW()' could fix the issue Commented May 8, 2014 at 14:25
  • RETURNING * is a PG feature which provides the values of the of the values of the created record; it is used by Rails in order to know which is the id of the created record. Commented May 8, 2014 at 14:51

2 Answers 2

1

i also got same problem when i check it seems i have nested transaction

if You got same problem try to find

ActiveRecord::Base.transaction

and remove it

because seems created_at doesn't assigned when You use transaction (maybe bug)

hope it helps

Sign up to request clarification or add additional context in comments.

Comments

0

It says: "null value in column "created_at" violates not-null constraint" when you run:

INSERT INTO "elements" ("updated_at") VALUES ('2014-05-08 13:27:56.002747')

Meaning that:

  • You're missing some kind of default value for the created_at field; or
  • You should set a created_at manually.

3 Comments

Sorry but don't both parts of your answer defy Rails' defaults? The standard for all my tabled models is t.datetime "created_at", null: false, with Rails setting the created_at value automatically whenever I create a new instance.
Might be. I'm merely posting what your error says and what it means though at thte Postgres level. I don't have the beginning of a clue of what Rails does or does not in the background.
This is a difference between Rails 4 and 5, apparently, and behavior varies between SQLite and PostGres e.g. For adding timestamps to existing tables in Rails 4, it's necessary to add_timestamps (without null: false) --and then change_column_null for the updated_at and created_at columns. See this post for more details: viget.com/articles/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.