0

I am trying to copy (partial) records between tables using ActiveRecord 3.2 and PostgreSQL under JRuby 1.7.

The guidance offered by this SO answer, is great and works under Ruby, but not under JRuby when operating with the activerecord-jdbcpostgresql-adapter.

My two models (Parties and Holders) have (nearly) identical attributes. I remove the common "id" column so it's not copied, and keep the intersection of the columns:

columns = (Party.column_names & Holder.column_names) - ["id"]

Then, I create new Party records based on specific Holder records:

parties_created = Party.create(Holder.where(:title_id => @title.id).all(:select => columns.join(",") ).map(&:attributes))

And, even though I confirm that columns does not include the "id" column

columns are: ["first_name", "middle_name", "other_name"]

the jdbc adapter insists on including it:

SQL (11.0ms) INSERT INTO "parties" ("first_name", "id", "middle_name", "other_name") VALUES ('JOHN', NULL, 'HENRY', 'SMITH') RETURNING "id" ActiveRecord::JDBCError: org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint

How do I tell the adapter to keep the "id" column out of things? Can anyone suggest a work-around?

Thanks, Tim

Community
  • 1
  • 1
tjg
  • 105
  • 1
  • 7
  • What happens if you manually `Party.create(:first_name => 'a', :middle_name => 'b', :other_name => 'c')`? – mu is too short Aug 07 '14 at 04:09
  • The JDBC adapter should do nothing of the sort. It'll add `RETURNING "id"` but it'll never alter the `INSERT` list. Something at a higher level in the stack is generating that query, and that's where you need to look. – Craig Ringer Aug 07 '14 at 04:12

0 Answers0