This is a simplified toy example to illustrate my question.
The Orders table has 2 columns, :gateway_a_id and :gateway_b_id, respectively.
The Gateways table has 1 column, :gateway_type
Before creating an Order, 2 gateway records need to be created for each gateway_type, A and B, respectively.
On the UI, the Orders form contains 2 select menus, Gateway A and Gateway B, respectively.
When trying to delete a gateway record that is associated with an order, how can I resolve the following error:
Ecto.ConstraintError at DELETE /gateways/2
constraint error when attempting to delete struct:
* foreign_key: orders_gateway_type_a_id_fkey
If you would like to convert this constraint into an error,
please call foreign_key_constraint/3 in your changeset and
define the proper constraint name. The changeset has not defined any constraint.
note for below: (on_delete: :nothing
is intentionally used because the user must manually update the order to remove gateway type references before deleting a gateway)
def change do
create table(:orders) do
add :name, :string
add :gateway_type_a_id, references(:gateways, on_delete: :nothing)
add :gateway_type_b_id, references(:gateways, on_delete: :nothing)
timestamps()
end
create index(:orders, [:gateway_type_a_id])
create index(:orders, [:gateway_type_b_id])
end
def change do
create table(:gateways) do
add :name, :string
add :gateway_type, :string
timestamps()
end
# do I create 2 indices on gateways?
end
schema "orders" do
field :name, :string
field :gateway_type_a_id, :id
field :gateway_type_b_id, :id
end
schema "gateways" do
field :gateway_type, :string
end
Note to moderator: Since this is Ecto related, I’m not sure if it belongs in Elixir or Phoenix.