Might be nice to have an option for ActiveRecord inserts and updates to use RETURNING *
instead of RETURNING <primary key>
.
Below is a small test case that shows why.
require 'active_record'
require 'logger'
# change to some test database already setup on your system
ActiveRecord::Base.establish_connection('postgres://localhost/some_database')
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.connection.execute <<-SQL
drop table if exists t;
create table t (
id serial primary key,
tomorrow timestamptz default now() + interval '1 day'
);
SQL
class T < ActiveRecord::Base
self.table_name = 't'
end
t = T.create!
p t
# t.tomorrow is nil :(
# SQL was: INSERT INTO "t" DEFAULT VALUES RETURNING "id"
p t.reload # it's there on reload though.
https://github.com/joevandyk/rails/commit/4497703f3190aed3bd0aa36609310642683a5c0a is a failing rails test case for this.
If postgresql modifies a column with a trigger, activerecord won't know about the changes until the model object is reloaded.
I'd love a way to be able to toggle this on and off on a case-by-case basis (there's going to be a performance regression if this is turned on all the time).
Please keep discussion and feature requests in the Rails Core mailing list.
Most helpful comment
If postgresql modifies a column with a trigger, activerecord won't know about the changes until the model object is reloaded.
I'd love a way to be able to toggle this on and off on a case-by-case basis (there's going to be a performance regression if this is turned on all the time).