Skip to content

Instantly share code, notes, and snippets.

@mazz
Created Oct 19, 2020
Embed
What would you like to do?
insert two users to the same org, second association clobbers first
iex(2)> org = Repo.get(Org, 1)
[debug] QUERY OK source="orgs" db=11.6ms decode=1.4ms queue=1.4ms idle=1333.3ms
SELECT o0."id", o0."basename", o0."shortname", o0."uuid", o0."hash_id", o0."inserted_at", o0."updated_at" FROM "orgs" AS o0 WHERE (o0."id" = $1) [1]
%Elijah.Schema.Org{
__meta__: #Ecto.Schema.Metadata<:loaded, "orgs">,
basename: "app",
channels: #Ecto.Association.NotLoaded<association :channels is not loaded>,
hash_id: "6vz6",
id: 1,
inserted_at: ~U[2020-10-19 16:27:51Z],
playlists: #Ecto.Association.NotLoaded<association :playlists is not loaded>,
shortname: "app",
updated_at: ~U[2020-10-19 20:28:31Z],
users: #Ecto.Association.NotLoaded<association :users is not loaded>,
uuid: "1f491f0b-e636-41ff-803f-f3eeca4ad872"
}
iex(3)> amos = Ecto.Query.from(u in User, where: u.username == "amos") |> Repo.one!()
[debug] QUERY OK source="users" db=2.0ms queue=1.4ms idle=545.4ms
SELECT u0."id", u0."email", u0."username", u0."name", u0."phone_number", u0."confirmed_at", u0."is_admin", u0."avatar", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."username" = 'amos') []
#Elijah.Schema.User<
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
avatar: nil,
confirmed_at: nil,
email: "amos@elijah.app",
id: 1,
inserted_at: ~N[2020-10-19 20:30:19],
is_admin: nil,
name: "Amos",
orgs: #Ecto.Association.NotLoaded<association :orgs is not loaded>,
phone_number: nil,
updated_at: ~N[2020-10-19 20:30:19],
username: "amos",
...
>
iex(4)> Accounts.add_user(%{"org_id" => org.id, "user_id" => amos.id})
[debug] QUERY OK source="orgs" db=4.3ms idle=317.5ms
SELECT o0."id", o0."basename", o0."shortname", o0."uuid", o0."hash_id", o0."inserted_at", o0."updated_at" FROM "orgs" AS o0 WHERE (o0."id" = $1) [1]
[debug] QUERY OK source="users" db=1.6ms queue=1.7ms idle=322.3ms
SELECT u0."id", u0."email", u0."username", u0."name", u0."phone_number", u0."confirmed_at", u0."is_admin", u0."avatar", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."id" = $1) [1]
[debug] QUERY OK source="users" db=2.4ms queue=1.9ms idle=332.6ms
SELECT u0."id", u0."email", u0."username", u0."name", u0."phone_number", u0."confirmed_at", u0."is_admin", u0."avatar", u0."inserted_at", u0."updated_at", o1."id" FROM "users" AS u0 INNER JOIN "orgs" AS o1 ON o1."id" = ANY($1) INNER JOIN "orgs_users" AS o2 ON o2."org_id" = o1."id" WHERE (o2."user_id" = u0."id") ORDER BY o1."id" [[1]]
[debug] QUERY OK db=0.3ms idle=346.8ms
begin []
[debug] QUERY OK db=2.2ms
INSERT INTO "orgs_users" ("org_id","user_id") VALUES ($1,$2) [1, 1]
[debug] QUERY OK db=0.5ms
commit []
%Elijah.Schema.Org{
__meta__: #Ecto.Schema.Metadata<:loaded, "orgs">,
basename: "app",
channels: #Ecto.Association.NotLoaded<association :channels is not loaded>,
hash_id: "6vz6",
id: 1,
inserted_at: ~U[2020-10-19 16:27:51Z],
playlists: #Ecto.Association.NotLoaded<association :playlists is not loaded>,
shortname: "app",
updated_at: ~U[2020-10-19 20:28:31Z],
users: [
#Elijah.Schema.User<
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
avatar: nil,
confirmed_at: nil,
email: "amos@elijah.app",
id: 1,
inserted_at: ~N[2020-10-19 20:30:19],
is_admin: nil,
name: "Amos",
orgs: #Ecto.Association.NotLoaded<association :orgs is not loaded>,
phone_number: nil,
updated_at: ~N[2020-10-19 20:30:19],
username: "amos",
...
>
],
uuid: "1f491f0b-e636-41ff-803f-f3eeca4ad872"
}
iex(5)> michael = User.registration_changeset(%User{username: "michael", name: "Michael"}, %{email: "michael@elijah.app", password: "password"}) |> Repo.insert()
[debug] QUERY OK db=3.3ms queue=2.0ms idle=525.5ms
INSERT INTO "users" ("email","name","username","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5) RETURNING "id" ["michael@elijah.app", "Michael", "michael", ~N[2020-10-19 20:34:21], ~N[2020-10-19 20:34:21]]
{:ok,
#Elijah.Schema.User<
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
avatar: nil,
confirmed_at: nil,
email: "michael@elijah.app",
id: 2,
inserted_at: ~N[2020-10-19 20:34:21],
is_admin: nil,
name: "Michael",
orgs: #Ecto.Association.NotLoaded<association :orgs is not loaded>,
phone_number: nil,
updated_at: ~N[2020-10-19 20:34:21],
username: "michael",
...
>}
iex(6)> michael = Ecto.Query.from(u in User, where: u.username == "michael") |> Repo.one!() [debug] QUERY OK source="users" db=1.4ms queue=1.0ms idle=1973.9ms
SELECT u0."id", u0."email", u0."username", u0."name", u0."phone_number", u0."confirmed_at", u0."is_admin", u0."avatar", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."username" = 'michael') []
#Elijah.Schema.User<
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
avatar: nil,
confirmed_at: nil,
email: "michael@elijah.app",
id: 2,
inserted_at: ~N[2020-10-19 20:34:21],
is_admin: nil,
name: "Michael",
orgs: #Ecto.Association.NotLoaded<association :orgs is not loaded>,
phone_number: nil,
updated_at: ~N[2020-10-19 20:34:21],
username: "michael",
...
>
iex(7)> Accounts.add_user(%{"org_id" => org.id, "user_id" => michael.id}) [debug] QUERY OK source="orgs" db=2.2ms idle=1053.1ms
SELECT o0."id", o0."basename", o0."shortname", o0."uuid", o0."hash_id", o0."inserted_at", o0."updated_at" FROM "orgs" AS o0 WHERE (o0."id" = $1) [1]
[debug] QUERY OK source="users" db=1.9ms idle=1055.6ms
SELECT u0."id", u0."email", u0."username", u0."name", u0."phone_number", u0."confirmed_at", u0."is_admin", u0."avatar", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."id" = $1) [2]
[debug] QUERY OK source="users" db=2.4ms idle=1057.8ms
SELECT u0."id", u0."email", u0."username", u0."name", u0."phone_number", u0."confirmed_at", u0."is_admin", u0."avatar", u0."inserted_at", u0."updated_at", o1."id" FROM "users" AS u0 INNER JOIN "orgs" AS o1 ON o1."id" = ANY($1) INNER JOIN "orgs_users" AS o2 ON o2."org_id" = o1."id" WHERE (o2."user_id" = u0."id") ORDER BY o1."id" [[1]]
[debug] QUERY OK db=0.2ms idle=1060.5ms
begin []
[debug] QUERY OK source="orgs_users" db=0.6ms
DELETE FROM "orgs_users" AS o0 WHERE ((o0."org_id" = $1) AND (o0."user_id" = $2)) [1, 1]
[debug] QUERY OK db=1.7ms
INSERT INTO "orgs_users" ("org_id","user_id") VALUES ($1,$2) [1, 2]
[debug] QUERY OK db=0.6ms
commit []
%Elijah.Schema.Org{
__meta__: #Ecto.Schema.Metadata<:loaded, "orgs">,
basename: "app",
channels: #Ecto.Association.NotLoaded<association :channels is not loaded>,
hash_id: "6vz6",
id: 1,
inserted_at: ~U[2020-10-19 16:27:51Z],
playlists: #Ecto.Association.NotLoaded<association :playlists is not loaded>,
shortname: "app",
updated_at: ~U[2020-10-19 20:28:31Z],
users: [
#Elijah.Schema.User<
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
avatar: nil,
confirmed_at: nil,
email: "michael@elijah.app",
id: 2,
inserted_at: ~N[2020-10-19 20:34:21],
is_admin: nil,
name: "Michael",
orgs: #Ecto.Association.NotLoaded<association :orgs is not loaded>,
phone_number: nil,
updated_at: ~N[2020-10-19 20:34:21],
username: "michael",
...
>
],
uuid: "1f491f0b-e636-41ff-803f-f3eeca4ad872"
}
iex(8)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment