Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Always return correct primary key from postgres

The previous implementation failed when the primary key
had no sequence (eg UUID)

Used the code from rails 3.2
  • Loading branch information...
commit ed30e77749a4cc3653c60fa08d23d512cfe1a02b 1 parent f2f25f2
@agios agios authored kares committed
Showing with 11 additions and 3 deletions.
  1. +11 −3 lib/arjdbc/postgresql/adapter.rb
View
14 lib/arjdbc/postgresql/adapter.rb
@@ -707,9 +707,17 @@ def sql_for_insert(sql, pk, id_value, sequence_name, binds)
[sql, binds]
end
+ # taken from rails postgresql_adapter.rb
def primary_key(table)
- pk_and_sequence = pk_and_sequence_for(table)
- pk_and_sequence && pk_and_sequence.first
+ result = select(<<-end_sql, 'SCHEMA')[0]
+ SELECT attr.attname
+ FROM pg_attribute attr
+ INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
+ WHERE cons.contype = 'p'
+ AND cons.conrelid = '#{quote_table_name(table)}'::regclass
+ end_sql
+
+ result && result["attname"]
end
# Returns an array of schema names.
@@ -1529,4 +1537,4 @@ def quote_column_name(name)
end
# Don't need to load native postgres adapter
-$LOADED_FEATURES << 'active_record/connection_adapters/postgresql_adapter.rb'
+$LOADED_FEATURES << 'active_record/connection_adapters/postgresql_adapter.rb'
Please sign in to comment.
Something went wrong with that request. Please try again.