Question: NOT NULL constraint failed created_at when saving model

Question

NOT NULL constraint failed created_at when saving model

Answers 0
Added at 2016-12-31 21:12
Tags
Question

I'm getting an exception when I call save! in a model.

SQLite3::ConstraintException at /contacts/import_spreadsheet/1/import NOT NULL constraint failed: contacts.created_at

In my schema, the model in question has:

t.datetime "created_at", null: false
t.datetime "updated_at", null: false

I checked my development database with SQLite Database Browser and the table has the created_at and updated_at columns as well.

I've tried many things, like setting the created_at property manually. If I do that, Rails tells me there's isn't a method created_at for that model.

My Contact class doesn't have anything special, just a belongs_to and two has_many associations. I tried to comment those to see if the problem was there, but it wasn't.

I already tried to stop the server, delete the sqlite databases and run rake db:setup.

First the problem was in a method that I use to import contacts. But then I realized when it occurs there, it occurs everywhere for that model, even in the simple crud rails scaffold generated for me.

The problem is that it's intermittent. Sometimes it happens, sometimes it doesn't. When I started writing it didn't work, but after restarting everything a few times, without changing anything, it's now working, I don't know for how long.

I noticed that when it doesn't work, the SQL generated doesn't have the created_at and updated_at attributes. So I think the problem is with rails itself, not my database or anything like that.

I also tried to update rails, so I'm now running rails 4.2.7.1.

Anyone else had any problem similar? created_at doesn't working intermittently?

-- UPDATE

I think I have found the line that causes the error:

contact.public_send("#{params[i.to_s]}=", row_value)

I'm using this because I receive parameters like "0"=>"name" so I know which object attribute I should set. Maybe it's something related to how ActiveRecord handles created_at and mt use of public_send?

Answers
Source Show
◀ Wstecz