Only change a few fields `ON CONFLICT` in an Upsert

Let's say I am implementing GitHub login myself. I have a User resource with a few attributes, and its create action is an upsert. Its working and Ash generates the right query, however, I want Ash to not include a few fields in the ON CONFLICT part. This is the query Ash generates:
INSERT INTO "users" AS u0 ("email","full_name","gh_access_token","gh_id","gh_scope","id","username") VALUES ($1,$2,$3,$4,$5,$6,$7) ON CONFLICT ("gh_id") DO UPDATE SET "email" = $8, "full_name" = $9, "gh_access_token" = $10, "gh_id" = $11, "gh_scope" = $12, "username" = $13 RETURNING ...
INSERT INTO "users" AS u0 ("email","full_name","gh_access_token","gh_id","gh_scope","id","username") VALUES ($1,$2,$3,$4,$5,$6,$7) ON CONFLICT ("gh_id") DO UPDATE SET "email" = $8, "full_name" = $9, "gh_access_token" = $10, "gh_id" = $11, "gh_scope" = $12, "username" = $13 RETURNING ...
I would instead want this (only two fields instead of all):
... ON CONFLICT ("gh_id") DO UPDATE SET "gh_access_token" = $10, "gh_scope" = $12 RETURNING ...
... ON CONFLICT ("gh_id") DO UPDATE SET "gh_access_token" = $10, "gh_scope" = $12 RETURNING ...
How would I go about doing this? This is my current action:
create :gh_login do
accept [:gh_id, :email, :username, :full_name, :gh_access_token, :gh_scope]

upsert? true
upsert_identity :unique_gh_id
end
create :gh_login do
accept [:gh_id, :email, :username, :full_name, :gh_access_token, :gh_scope]

upsert? true
upsert_identity :unique_gh_id
end
2 Replies
ZachDaniel
ZachDaniel3y ago
There is a tool for this, although perhaps a bit strangely named
Try this:
change fn changeset, _ ->
%{changeset | defaults: changeset.defaults ++ [:fields, :you, :dont, :want, :to, :change, :on, :upsert]}
end
Try this:
change fn changeset, _ ->
%{changeset | defaults: changeset.defaults ++ [:fields, :you, :dont, :want, :to, :change, :on, :upsert]}
end
I'd like to improve this in the future. And make it an explicit option, like upsert_fields [:foo, :bar]
Arjun Bajaj
Arjun BajajOP3y ago
Thanks!

Did you find this page helpful?