Arjun Bajaj
Arjun Bajaj
AEAsh Elixir
Created by Arjun Bajaj on 2/20/2023 in #support
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
6 replies
AEAsh Elixir
Created by Arjun Bajaj on 2/18/2023 in #support
How do you set an attribute to the join resource when creating?
For example, I have a User and an Org, joined by a many_to_many through a Join Resource UserOrg. UserOrg has an attribute :role. How do I set the role on the UserOrg when creating an Org?
create :create do
accept [:name]
argument :user_id, :binary

change fn changeset, %{actor: actor} = _context ->
changeset |> Ash.Changeset.set_argument(:user_id, actor.id)
end

change manage_relationship(:user_id, :users, type: :append_and_remove)
# ^^^ somehow set :role to :owner in this relationship
end
create :create do
accept [:name]
argument :user_id, :binary

change fn changeset, %{actor: actor} = _context ->
changeset |> Ash.Changeset.set_argument(:user_id, actor.id)
end

change manage_relationship(:user_id, :users, type: :append_and_remove)
# ^^^ somehow set :role to :owner in this relationship
end
26 replies
AEAsh Elixir
Created by Arjun Bajaj on 2/16/2023 in #support
Differences between ETS and Postgres
So my thinking right now is, I'm using ETS to build out all of my resources and basically the app for as long as I can get away with. I do realize there are some differences between the Postgres adapter and ETS such as when it comes to identities where you've to add pre_check_with. However, are there some major differences where Ash is not able to abstract away the data layer and the code would actually be different for the two? I just wanted to keep open the option of selecting a database later. Maybe PlanetScale (which is MySQL, but doesn't support foreign key constraints and has some other limitations too), or Postgres or SQLite. Of course I understand we'll have to figure out how to create a data layer for those as they don't exist in Ash right now, but maybe I could help with one of those if we decide to go that route?
17 replies