AE
Ash Elixir•2y ago
Blibs

builk_create upsert seems to be broken

I have the following create in my resource:
create :create do
description "Creates a new predefined school"

primary? true

accept [:name, :uid, :geography, :address, :city, :district, :state, :country, :type]

upsert? true
upsert_identity :unique_uid
upsert_fields [:name, :geography, :address, :city, :district, :state, :country, :type]
end
create :create do
description "Creates a new predefined school"

primary? true

accept [:name, :uid, :geography, :address, :city, :district, :state, :country, :type]

upsert? true
upsert_identity :unique_uid
upsert_fields [:name, :geography, :address, :city, :district, :state, :country, :type]
end
the upsert_identity comes from this identity:
identity :unique_uid, [:uid]
identity :unique_uid, [:uid]
Just so you know, the :uid field is not a primary key. If I try to insert a row with the same uid as one that already exists in the DB using the create funciton, it works as expected and I get the ON CONFLICT ("uid") in the SQL query. Now, if I try to do the same, but with a bulk_create call, the resulti SQL query will contain ON CONFLICT ("id"), in other words, it uses id instead of uid which was set by the upsert_identity function.
10 Replies
ZachDaniel
ZachDaniel•2y ago
Interesting...so its ignoring the upsert_identity on the action? what if you pass upsert_identity :unique_uid option when calling the bulk create?
Blibs
BlibsOP•2y ago
Like this right?
Onboarding.bulk_create(schools, Onboarding.PredefinedSchool, :create, upsert_identity: :unique_uid)
Onboarding.bulk_create(schools, Onboarding.PredefinedSchool, :create, upsert_identity: :unique_uid)
It still ignores it:
INSERT INTO "predefined_schools" ("address","city","country","district","geography","id","inserted_at","name","state","type","uid","updated_at") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name","geography" = EXCLUDED."geography","address" = EXCLUDED."address","city" = EXCLUDED."city","district" = EXCLUDED."district","state" = EXCLUDED."state","country" = EXCLUDED."country","type" = EXCLUDED."type" ["600 E Alabama Ave - Albertville, 35950", "Albertville", "United States", "Albertville City", nil, "e9e9c6c5-20cc-4cb6-92cf-f444653d4645", ~U[2023-06-11 15:31:51.697349Z], "Albertville Middle School", "AL", :public, "usa_public_010000500870", ~U[2023-06-11 15:31:51.697349Z]]
INSERT INTO "predefined_schools" ("address","city","country","district","geography","id","inserted_at","name","state","type","uid","updated_at") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name","geography" = EXCLUDED."geography","address" = EXCLUDED."address","city" = EXCLUDED."city","district" = EXCLUDED."district","state" = EXCLUDED."state","country" = EXCLUDED."country","type" = EXCLUDED."type" ["600 E Alabama Ave - Albertville, 35950", "Albertville", "United States", "Albertville City", nil, "e9e9c6c5-20cc-4cb6-92cf-f444653d4645", ~U[2023-06-11 15:31:51.697349Z], "Albertville Middle School", "AL", :public, "usa_public_010000500870", ~U[2023-06-11 15:31:51.697349Z]]
ZachDaniel
ZachDaniel•2y ago
🤔 we have multiple tests for this behavior are you on the latest of everything?
Blibs
BlibsOP•2y ago
Yep, latest version:
$ mix hex.outdated
Dependency Current Latest Status
ash 2.9.27 2.9.27 Up-to-date
ash_authentication 3.11.3 3.11.3 Up-to-date
ash_authentication_phoenix 1.7.2 1.7.2 Up-to-date
ash_postgres 1.3.30 1.3.30 Up-to-date
$ mix hex.outdated
Dependency Current Latest Status
ash 2.9.27 2.9.27 Up-to-date
ash_authentication 3.11.3 3.11.3 Up-to-date
ash_authentication_phoenix 1.7.2 1.7.2 Up-to-date
ash_postgres 1.3.30 1.3.30 Up-to-date
ZachDaniel
ZachDaniel•2y ago
what if you remove the upsert_identity from the action itself? And just pass upsert?: true and upsert_identity as the option?
Blibs
BlibsOP•2y ago
If I do that it works fine
ZachDaniel
ZachDaniel•2y ago
gotcha, okay, so using the action upsert_identity is what causes the problem can you an open an issue for this on ash? I think its an ash core thing
Blibs
BlibsOP•2y ago
GitHub
bulk_create ignores upsert_identity if set directly in the action...
Describe the bug Setting a upsert_identity to the action will be ignored by a bulk_create function call. To Reproduce Just add a upsert_identity to a create action and then try to run bulk_create. ...
ZachDaniel
ZachDaniel•2y ago
can you try main?
Blibs
BlibsOP•2y ago
Yep, working on main 🚀

Did you find this page helpful?