1

I want to manually set value to field "ID" in my table which is not a primary key. Everytime I use the .id attribute and assign value to it, it gets assigned to the primary key (abc_id) but not "id"

table/model: MyModel
primary key: abc_id
another column: id

i tried following ways of saving an ID but none of them seemed to work.

test = MyModel.new
test.id = 555 
==> updates abc_id
test.write_attribute(:id, 555)
 ==> private method not available
test.update_attribute(:id,555)
==> updates abc_id -->ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR:  null value in column "id" violates not-null constraint
test.attributes = {id: 555} 
==> no error but doesn't do anything
test.send(:attributes=, id: 555) 
==> saves other attributes but not id
 => nil 

How can I assign value to "id" since I always get PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint error while trying to save the record

KavitaC
  • 635
  • 7
  • 29

1 Answers1

0

Does this answer help?

How to set primary key in ruby on rails migration?

In my experience this shouldn't be necessary though.

Community
  • 1
  • 1
pip36
  • 126
  • 1
  • 8