In my Phoenix app, I have a Contact table and an Address table. The contact schema is:
schema "contacts" do
field :name, :string
field :number, :string
has_one :address, App.Address, on_delete: :nothing
timestamps()
end
The address schema is
schema "addresses" do
field :name, :string
field :lat, :decimal
field :lng, :decimal
timestamps()
end
I just want to have address_id in contacts be selected via a dropdown of addresses filled from the database. However, phoenix is returning
`App.Address.contact_id` in `where` does not exist in the schema in query:
from a in App.Address,
where: a.contact_id == ^9435,
select: {a.contact_id, a}
So it is trying to find the contact_id field in address, however I want address to belong to a contact or many contacts, but I don't want the contact_id field in the address table. How is this to be accomplished with ecto associations?
I am aware I can do this with an intermediary table that has an entry for each contact_id and address_id paired up, but I would rather avoid doing so because I think that adds a level of unnecessary complexity and abstraction.
EDIT: Thank you for the answer, I realized I just needed to think of it in the reverse just a few minutes after posting. When thinking of it as an address has many contacts and a contact has one address the relationships make sense. I was just stuck thinking that address belongs to a contact when that is not necessarily true.