Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Should ActiveRecord use 'RETURNING *' instead of 'RETURNING id' on inserts/updates? #14266

Closed
joevandyk opened this Issue · 3 comments

2 participants

@joevandyk

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.
@joevandyk

joevandyk@4497703 is a failing rails test case for this.

@joevandyk

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

@rafaelfranca

Please keep discussion and feature requests in the Rails Core mailing list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.