Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when creating no id is set #13

Open
techbrownbags opened this issue Oct 29, 2018 · 2 comments
Open

when creating no id is set #13

techbrownbags opened this issue Oct 29, 2018 · 2 comments

Comments

@techbrownbags
Copy link

I have an EXISTING redshift DB i'm using rails 5 against. Things are working great except when creating, in this case no id is set in the activerecord object.
I've tried save and create
Also, tried using some callbacks, setting id etc. I can't get max then set id because I'm using a identity column.

Here's a snippet from the table
id| integer| not null default "identity"(320804, 0, ('0,1'::character varying)::text)
"app_users_pkey" PRIMARY KEY, btree (id)

@techbrownbags
Copy link
Author

AFAIK this occurs because on create the id is not returned since REDSHIFT does not support RETURNING ID on INSERT. I'm either going to branch or remove meta-data from our redshift cluster.

To fix you would need to do a select on a unique index after the insert occurs.

Any other ideas?

@techbrownbags
Copy link
Author

fyi for anyone else reading this the problem is in
lib/active_record/connection_adapters/redshift_adapter.rb
def use_insert_returning?
false
end

and in activerecord rails/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb

def sql_for_insert(sql, pk, id_value, sequence_name, binds)
unless pk
# Extract the table from the insert sql. Yuck.
table_ref = extract_table_ref_from_insert_sql(sql)
pk = primary_key(table_ref) if table_ref
end

      if pk && use_insert_returning?
        sql = "#{sql} RETURNING #{quote_column_name(pk)}"
      end 
      [sql, binds]
    end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant