Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to handle foreign key constraint errors on deleting #4587

Closed
cbishopvelti opened this issue Mar 2, 2025 · 3 comments
Closed

Unable to handle foreign key constraint errors on deleting #4587

cbishopvelti opened this issue Mar 2, 2025 · 3 comments
Labels

Comments

@cbishopvelti
Copy link

cbishopvelti commented Mar 2, 2025

Elixir version

1.18.1

Database and Version

{:exqlite, "0.29.0"} {:ecto_sqlite3, "0.18.1"}

Ecto Versions

3.12.1

Database Adapter and Versions (postgrex, myxql, etc)

{:exqlite, "0.29.0"} {:ecto_sqlite3, "0.18.1"}

Current behavior

Create a two tables,
create table(:venue) do
end
create table(:event) do
add :venue_id, references(
:venue
on_delete: :restrict
)
end
Try and delete a venue:
Venue |> Ecto.Changeset.change() |> Ecto.Changeset.no_assoc_constraint(:event) |> Repo.delete()
I get a runtime error:

#PID<0.750.0> running Phoenix.Endpoint.SyncCodeReloadPlug (connection #PID<0.749.0>, stream id 1) terminated
Server: 127.0.0.1:4000 (http)
Request: POST /api/graphql
** (exit) an exception was raised:
    ** (Ecto.ConstraintError) constraint error when attempting to delete struct:

    * nil (foreign_key_constraint)

If you would like to stop this constraint violation from raising an
exception and instead add it as an error to your changeset, please
call `foreign_key_constraint/3` on your changeset with the constraint
`:name` as an option.

The changeset defined the following constraints:

    * "event_venue_id_fkey" (foreign_key_constraint)

Expected behavior

The error should go into the changeset.

There doesn't appear to be a way to match against "* nil (foreign_key_constraint)" as if I add the opt name: nil, it will use the default generated fk name.

@v0idpwn
Copy link
Member

v0idpwn commented Mar 2, 2025

Hi, @cbishopvelti, thanks for reporting.

This is due to a limitation in exqlite/ecto_sqlite3. It's tracked here: elixir-sqlite/ecto_sqlite3#124 and someone commented a workaround that maybe works for you.

@josevalim
Copy link
Member

Thank you @v0idpwn, please refer to the ecto_sqlite3 issue then. If there is something that needs to be done on Ecto @warmwaffles, please let us know!

@josevalim josevalim closed this as not planned Won't fix, can't repro, duplicate, stale Mar 2, 2025
@warmwaffles
Copy link
Member

I believe this is mostly all in sqlite's realm. I'll need to play with the error returns and figure out if there is a way to make this play nicely with the other adapters. I don't have the link, but on sqlite's forum there were a few places the FK error formatting should be changed but upstream won't because it's been formatted the way it has for years.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants