1

I followed the setup instructions for a Rails project I've been handed to work on. However, after creating the database and running migrations with bundle exec, the db/schema.rb changes. The main change is that the timestamps created_at and updated_at lose their null: false constraint. e.g.:

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

The project is Rails 4.2 & Ruby 2.4.1.
However, it was originally written with Rails 3.2 and Ruby 1.9.2, so there's the possibility that some of the older code/config is in play(?)
I'm using PostgreSQL: 'pg', '~> 0.17'.

So: does anyone know why the null: false has left the schema?
I'd very much like to keep the null: false constraints, and I guess I can add them back in a migration, but I'm very puzzled why the constraints have disappeared from the schema.

edited to add:
- In the migrations, where t.timestamps are defined, there isn't a null: false constraint
- There is no null constraint on the fields in the actual database.
Could earlier versions of ActiveRecord have added the null constraint by default?

I've looked around for answers, but not finding much. This rails issue reports the same thing, but for Oracle.
Here on Stack Overflow, mysql - Rails: differences in db/schema.rb - null: false at created_at/updated_at columns is the same problem, but the suggested solution (drop DB and recreate) didn't help me.

kbkbkb
  • 11
  • 2
  • I seem to remember the `:null` option for `t.timestamps` changing defaults at some point. Check what the migrations you ran say and check what the columns look like inside the database (use `psql` to connect to the database and then `\dt your_table` to see what the tables really look like). – mu is too short Sep 28 '18 at 03:02
  • Thanks @muistooshort. I should have put this in the original question (I edited it in above) - when I connect to the database directly, there's no null constraint on those fields. Maybe it's a matter of changing ActiveRecord defaults/conventions? However if so, I can't find any documentation about it. – kbkbkb Sep 30 '18 at 20:28
  • Check the migrations for `t.timestamps` calls, they probably don't say anything about the `:null` option so they'd be subject to the default change that I mentioned before (but honestly I'm not certain about that, I almost never use the default for `:null` because it is usually the wrong thing). If the migrations don't saying anything about `:null` then fix them or, better, ignore the migrations and create your database using `schema.rb` rather than the migrations (and then delete the migrations since they're not meant to be permanent anyway). – mu is too short Sep 30 '18 at 20:59

0 Answers0