I have 2 tables:
user:
id
username
password
unique_index username
(the schema has a has_many other)
other:
id
user_id - references(:users)
foo
index user_id
(the schema has a belongs_to user)
in the changeset for "Other" i have this
model
|> cast(params, @req, @opt)
|> foreign_key_constraint(:user_id)
My assumption at this point was the "Other" ecto model requires a "User" to be associated with it to exist (which is what I want)
But my second assumption was if I delete the "User" record then all associated "Other" records would be deleted (via a Cascade delete)
What actually happens is I have a Ecto.ConstraintError when trying to delete a "User" record (I'm assuming because there is an "Other" record associated to that User)
So how would I go about having it work in the way I want which is:
- A "user" can be created standalone
- An "other" can be created but must belong to a "user"
- When an "other" is deleted it doesn't affect anything else
- When a "user" is deleted it deletes all associated "other" records too
Essentially a Cascade Delete on the User for any items that references it