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