I have been looking for a solution to this problem since couple of hours now. It seems very simple but for some reason I can't get it to work.
I have a model, lets call it MyModel
. It has 3 columns, :id
, :column_a
, :relationship_id
:relationship_id
is a foreign key with not null constraint.
I have 4 records in the database already like such:-
:id, :column_a, :relationship_id,
1, 1, 7
2, 2, 8
3, 3, 9
4, 4, 10
I want to change the values of :column_a
, using the following data
data = [{"id"=>"1", "column_a"=>4},
{"id"=>"2", "column_a"=>3},
{"id"=>"3", "column_a"=>2},
{"id"=>"4", "column_a"=>1}]
But this data is missing :relationship_id
column values which I want to keep unchanged.
When I do something like MyModel.upsert_all(data, update_only: :column_a)
I get the following error
ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR: null value in column "relationship_id" of relation "my_models" violates not-null constraint
DETAIL: Failing row contains (1, 4, null).
I have tried update_all
and raw SQL but the problem remains the same. The only way I can get it working is by individually updating each record which is not efficient.
Maybe I am doing something wrong? Any ideas?