From 3b1e60aa0ae16d1c6e3e753b0861e6d7c173356c Mon Sep 17 00:00:00 2001 From: Rishi Ranjan Date: Fri, 9 Jan 2026 09:03:21 +1100 Subject: [PATCH] Map postgres :restrict_violation to :foreign_key constraint In PostgreSQL 18, this error code is now used for `ON DELETE RESTRICT` constraint violation. The existing `:foreign_key_violation` error code is raised for `ON DELETE NO ACTION`. The behaviour changed in postgres commit https://github.com/postgres/postgres/commit/086c84b23d99c2ad268f97508cd840efc1fdfd79 The change is backwards compatible as the error code was not previously raised even though it existed in the postgres documentation. It is now correctly raised for the `ON DELETE RESTRICT` constraint. --- lib/ecto/adapters/postgres/connection.ex | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/ecto/adapters/postgres/connection.ex b/lib/ecto/adapters/postgres/connection.ex index d8f063f7..7de5bb1f 100644 --- a/lib/ecto/adapters/postgres/connection.ex +++ b/lib/ecto/adapters/postgres/connection.ex @@ -28,6 +28,12 @@ if Code.ensure_loaded?(Postgrex) do ), do: [foreign_key: constraint] + def to_constraints( + %Postgrex.Error{postgres: %{code: :restrict_violation, constraint: constraint}}, + _opts + ), + do: [foreign_key: constraint] + def to_constraints( %Postgrex.Error{postgres: %{code: :exclusion_violation, constraint: constraint}}, _opts