Rails: Should ActiveRecord use 'RETURNING *' instead of 'RETURNING id' on inserts/updates?

Created on 4 Mar 2014  路  3Comments  路  Source: rails/rails

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.

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).

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings