Sorry if this has been answered before but I can't find a definite answer.
Surely someone has come across this issue when using upsert
you need to specify every column including created_at
and updated_at
?
How can I not update the created_at
timestamp when an upsert happens? I only want to insert created_at
once.
a = User.upsert({
name: ....,
age: ....
created_at: DateTime.now,
updated_at: DateTime.now
}, unique_by: :upsert_user_index )
Update Existing tables with defaults:
change_column :table_foos, :created_at, :datetime, null: false, default: -> { "CURRENT_TIMESTAMP" }
change_column :table_foos, :updated_at, :datetime, null: false, default: -> { "CURRENT_TIMESTAMP" }
Create NEW tables with defaults:
create_table :table_foos do |t|
# date with timestamp
t.datetime :last_something_at, null: false, default: -> { "CURRENT_TIMESTAMP" }
# standard timestamps
t.timestamps default: -> { "CURRENT_TIMESTAMP" }
end
No need to provide created_at
and updated_at
if you had already set the default
value i.e. now()
in the db. It will automatically takes the current timestamp.
a = User.upsert({
name: ....,
age: ....,
created_at: Time.now,
updated_at:Time.now
}, nil, unique_by: :upsert_user_index )
But if you want to insert a single record, you should use create
or save
. It will trigger the validations and callbacks. And if you want to skip them, you can do as well.
You should use upsert_all
if want to insert multiple entries in single sql without callbacks and validations.
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