chrisdel101

chrisdel101

I’ve spent several days trying to make my this work, connecting my tables using Ecto Association Guide – Ecto v2.2.11

I think Many-to-many associations in phoenix and ecto - #5 by marick might work.

But now my embedded schema is screwing things up. It is not part of the DB, and it only used for the UI, but I think I still need it.

I thought embedded schemas were not supposed to effect the DB? Is there an way around this?


# this works
org = %{name: "Toys R Us", slug: "toys-r-us"}
org_c = Organization.changeset(%Organization{},  org)
org_insert =  Repo.insert!(org_c) |> Repo.preload(:employees)  

# this does not - error is due to the .preload(:organizations)
emp = %{last_name: "schmo", first_name: "joe", role: "owner", email: "joe@schmo.com", password: "password"}
emp_c = Employee.registration_changeset(%Employee{}, emp)
emp_insert = Repo.insert!(emp_c) |> TurnStile.Repo.preload(:organizations)
# embedded schema doesn't exist in DB
** (Postgrex.Error) ERROR 42703 (undefined_column) column o0.owner_employee does not exist

# schema with embeds
schema "organizations" do
    field :email, :string
    field :name, :string
    field :slug, :string
    field :phone, :string
    # org has many employees; employees can belong to many organiztions (mostly for owners)
    many_to_many :employees, Employee, join_through: "organization_employees"
 

    embeds_one :owner_employee, Employee do
      field :first_name, :string
      field :last_name, :string
      field :_email, :string
      field :password, :string, virtual: true, redact: true
    end

schema "employees" do
    field :first_name, :string
    field :last_name, :string
    field :role, :string
    field :email, :string
    field :password, :string, virtual: true, redact: true
    field :confirmed_at, :naive_datetime
    # org has many employees within the company; employees belongs to many orgs
    many_to_many :organizations, Organization, join_through: "organization_employees"

# join table I want to use, but don't know how to use
defmodule Repo.Migrations.OrganizationEmployees do
  use Ecto.Migration

  def change do
    create table(:organization_employees) do
      add :organization_id, references(:organizations)
      add :employee_id, references(:employees)

      timestamps()
    end
    create unique_index(:organization_employees, [:organization_id, :employee_id])

  end
end

Showing Posts 1 to 4

cevado

cevado

you misunderstood what embedded schemas are. they’re a way to describe a structured jsonb/map field.
if you don´t have a jsonb column named owner_employee you’d be better creating a virtual field(virutal fields are the ones that doesn’t exist in the db) that is a map.

for reference:

chrisdel101

chrisdel101 OP

Perhaps I do misunderstand. So these things don’t apply to me? It’s what I thought based on Embedded Schemas — Ecto v3.14.0

  • This data can live in memory, or can be stored in the database. (For me it’s memory)
  • You are maintaining intermediate-state data, like when UI form fields map onto multiple tables in a database. (I’m mapping to tables organizations, employees, and organization_employees)
  • If you wish to save your embedded schema to the database, you need to write a migration to include the embedded data. (I don’t wish to save it)

I don’t have owner_employee but I do have a join table that I’m trying, and failing, to use.

dimitarvp

dimitarvp

Embedded schemas without being saved to DB are basically a way to parse data by using Ecto constructs, more or less.

But if you start actively using them with other records that hit the DB you’ll have trouble, yeah. Either have them be virtual attributes (where they are used in other schemas) and/or just add them after a Repo.get and such.

cevado

cevado

so, there are a few ways to deal with that:

  • make the field in organizations a virtual one of the type map. being it a map, you can load whatever map you want there, even a employee struct loaded from the database.
  • if there is a query to define which row from employees is a owner_employee you can actually make it a relation… something like
schema "organizations" do
  ...
  has_one :owner_employee, Employee, through: "organization_employees", where: [is_owner: true]
end

and you can preload the owner_employee the same way you preload any other relation.

when you use a embedded schema this way inside another schema is always to describe a structured jsonb field.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews