Some time ago we upgraded our application to Rails 4 and switched to JRuby.
Before that change migrations created the default timestamps as NOT NULL. After that change the NOT NULL is missing.
We create these timestamps (created_at, updated_at) as follows:
class Model < ActiveRecord::Migration
def change
create_table :model do |t|
t.belongs_to :user, :null => false
t.text :content
t.timestamps
end
end
end
The important parts of our application are:
Do you have any idea what might cause the problem and how we can change the default generation back to NOT NULL?
I don't know if it is documented anywhere but the source indicates that you can pass the usual column options to t.timestamps
:
# Appends <tt>:datetime</tt> columns <tt>:created_at</tt> and
# <tt>:updated_at</tt> to the table.
def timestamps(*args)
options = args.extract_options!
column(:created_at, :datetime, options)
column(:updated_at, :datetime, options)
end
so you can say:
create_table :model do |t|
#...
t.timestamps :null => false
end
and your columns should be NOT NULL.
If you look at the 3.2 version, you'll see what's happened:
def timestamps(*args)
options = { :null => false }.merge(args.extract_options!)
#...
end
so 3.2 creates the timestamp columns as NOT NULL by default but 4.0 does not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With