Skip to content

Instantly share code, notes, and snippets.

@chrisdel101
Last active May 23, 2023 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdel101/a5c870c19cf8013e4c9842c210e47781 to your computer and use it in GitHub Desktop.
Save chrisdel101/a5c870c19cf8013e4c9842c210e47781 to your computer and use it in GitHub Desktop.
Loading put_assoc
# org params
org = %{
"email" => "toys@rus.com",
"name" => "Toys R Us",
"phone" => "777777777",
"slug" => "toys-r-us"
}
# make org changeset
org_changeset = Organization.changeset(%Organization{}, o)
# #Ecto.Changeset<
# action: nil,
# changes: %{
# email: "toys@rus.com",
# name: "Toys R Us",
# phone: "777777777",
# slug: "toys-r-us"
# },
# errors: [],
# data: #Organization<>,
# valid?: true
# >
# insert org and preload
org_insert = Repo.insert!(org_changeset) |> Repo.preload(:employees)
# %Organization{
# __meta__: #Ecto.Schema.Metadata<:loaded, "organizations">,
# id: 108,
# email: "toys@rus.com",
# name: "Toys R Us",
# slug: "toys-r-us",
# phone: "777777777",
# employees: [],
# inserted_at: ~N[2023-05-23 17:42:04],
# updated_at: ~N[2023-05-23 17:42:04]
# }
# employee params
e = %{
"email" => "joe@schmo.com",
"email_confirmation" => "joe@schmo.com",
"first_name" => "Joe ",
"last_name" => "Schmo",
"password" => "password",
"password_confirmation" => "password",
"role" => "owner"
}
# make employee changeset
e_changeset = Employee.registration_changeset(%Employee{}, emp)
# #Ecto.Changeset<
# action: nil,
# changes: %{
# email: "joe@schmo.com",
# first_name: "Joe ",
# hashed_password: "**redacted**",
# last_name: "Schmo",
# role: "owner"
# },
# errors: [],
# data: #Employee<>,
# valid?: true
# >
# insert and preload
e_insert = Repo.insert!(e_changeset) |> Repo.preload(:organizations)
# #Employee<
# __meta__: #Ecto.Schema.Metadata<:loaded, "employees">,
# id: 30,
# first_name: "Joe ",
# last_name: "Schmo",
# role: "owner",
# client_type: nil,
# email: "joe@schmo.com",
# confirmed_at: nil,
# organizations: [],
# users: #Ecto.Association.NotLoaded<association :users is not loaded>,
# alerts: #Ecto.Association.NotLoaded<association :alerts is not loaded>,
# owner: #Ecto.Association.NotLoaded<association :owner is not loaded>,
# inserted_at: ~N[2023-05-23 16:08:40],
# updated_at: ~N[2023-05-23 16:08:40],
# ...
# >
# build table assoc
org_update = Ecto.Changeset.put_assoc(org_changeset, :employees, [e_insert])
# #Ecto.Changeset<
# action: nil,
# changes: %{
# email: "toys@rus.com",
# employees: [
# #Ecto.Changeset<action: :update, changes: %{}, errors: [],
# data: #Employee<>, valid?: true>
# ],
# name: "Toys R Us",
# phone: "777777777",
# slug: "toys-r-us"
# },
# errors: [],
# data: #Organization<>,
# valid?: true
# >
# try to run assoc - FAIL
Repo.update(org_update)
# ** (Ecto.NoPrimaryKeyValueError) struct `%Organization{__meta__: #Ecto.Schema.Metadata<:built, "organizations">, id: nil, email: nil, name: nil, slug: nil, phone: nil, employees: #Ecto.Association.NotLoaded<association :employees is not loaded>, inserted_at: nil, updated_at: nil}` is missing primary key value
# (ecto 3.9.4) lib/ecto/repo/schema.ex:977: anonymous fn/3 in Ecto.Repo.Schema.add_pk_filter!/2
# (elixir 1.14.1) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
# (ecto 3.9.4) lib/ecto/repo/schema.ex:416: Ecto.Repo.Schema.do_update/4
# iex:12: (file)
select * from organizations;
id | name | slug | email | phone | inserted_at | updated_at
----+-----------+-----------+--------------+-----------+---------------------+---------------------
108 | Toys R Us | toys-r-us | toys@rus.com | 777777777 | 2023-05-23 16:41:19 | 2023-05-23 16:41:19
(1 row)
# OTHER DETAILS
Organization shcema & migration
schema "organizations" do
field :email, :string
field :name, :string
field :slug, :string
field :phone, :string
many_to_many :employees, Employee, join_through: "organization_employees"
....
def change do
create table(:organizations) do
add :name, :string
add :slug, :string
add :email, :string
add :phone, :string
timestamps()
end
end
Employee schema & migration - unrelated relationships are shown in case they are error causing
schema "employees" do
field :first_name, :string
field :last_name, :string
field :role, :string
field :client_type, :string
field :email, :string
field :password, :string, virtual: true, redact: true
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime
many_to_many :organizations, Organization, join_through: "organization_employees"
# all users created by an employee
has_many :users, User
# all alerts created by an employee
has_many :alerts, Alert
# any employees that is an owner
has_one :owner, Owner
create table(:employees) do
add :first_name, :string
add :last_name, :string
add :client_type, :string
add :role, :string, null: false
add :email, :citext, null: false
add :hashed_password, :string, null: false
add :confirmed_at, :naive_datetime
OrganizationEmployees migration
def change do
create table(:organization_employees) do
add :organization_id, references(:organizations)
add :employee_id, references(:employees)
timestamps()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment