stiang

stiang

Return changeset with constraint error from nested transaction

I have a “smart” put function that takes care of saving (creating or updating) a resource and all its sub resources automatically. It works by recursively calling itself for each sub resource (as deeply as needed).

The database operations are wrapped in a Repo.transaction/1 function, which means that when the put function calls itself, the transactions get nested.

Whenever something fails when saving a sub resource, I want the outer transaction to fail and return some information about what went wrong, typically a changeset with errors.

This works in the general case, but not when a check_constraint/3 results in a changeset with errors. In the example below, the StayPolicyPeriod changeset function has the following validation:

cast(entity, [...])
|> check_constraint(:opens_at, name: "stay_policy_periods_opens_before_start")

This works as it should (setting an error on the changeset instead of causing an exception), but it also seems to trigger a generic rollback which doesn’t return any information.

I’ve created a small script to illustrate the issue:

defmodule TestNestedTransactions do
  require Logger
  import Ecto.Query, warn: false

  alias ApiServer.Repo
  alias ApiServer.V1.Geo.Country
  alias ApiServer.V1.Stays.StayPolicy
  alias ApiServer.V1.Stays.StayPolicyPeriod

  def test_country do
    outer_transaction_result =
      Repo.transaction(fn ->
        inner_transaction_result =
          Repo.transaction(fn ->
            Country.changeset(%Country{}, %{
              name: "Test Country",
              code: "TEST",
              enabled: true
            })
            |> Repo.insert()
          end)

        Logger.info("COUNTRY inner transaction result: #{inspect(inner_transaction_result)}\n")
        inner_transaction_result
      end)

    Logger.info("COUNTRY outer transaction result: #{inspect(outer_transaction_result)}\n")
    outer_transaction_result
  end

  def test_stay_policy_period do
    outer_transaction_result =
      Repo.transaction(fn ->
        inner_transaction_result =
          Repo.transaction(fn ->
            some_stay_policy = from(sp in StayPolicy, limit: 1) |> Repo.one()

            StayPolicyPeriod.changeset(%StayPolicyPeriod{}, %{
              stay_policy_id: some_stay_policy.id,
              period: %Postgrex.Range{
                lower: ~D[2022-01-10],
                upper: ~D[2022-02-01]
              },
              opens_at: ~U[2022-01-20 12:00:00Z]
            })
            |> Repo.insert()
          end)

        Logger.info(
          "STAY POLICY PERIOD inner transaction result: #{inspect(inner_transaction_result)}\n"
        )

        inner_transaction_result
      end)

    Logger.info(
      "STAY POLICY PERIOD outer transaction result: #{inspect(outer_transaction_result)}\n"
    )

    outer_transaction_result
  end
end

TestNestedTransactions.test_country()
TestNestedTransactions.test_stay_policy_period()

which results in the following output:

[info] COUNTRY inner transaction result: {:ok, {:error, #Ecto.Changeset<action: :insert, changes: %{code: "TEST", enabled: true, name: "Test Country"}, errors: [phone_code: {"can't be blank", [validation: :required]}], data: #ApiServer.V1.Geo.Country<>, valid?: false, ...>}}
[info] COUNTRY outer transaction result: {:ok, {:ok, {:error, #Ecto.Changeset<action: :insert, changes: %{code: "TEST", enabled: true, name: "Test Country"}, errors: [phone_code: {"can't be blank", [validation: :required]}], data: #ApiServer.V1.Geo.Country<>, valid?: false, ...>}}}

[info] STAY POLICY PERIOD inner transaction result: {:ok, {:error, #Ecto.Changeset<action: :insert, changes: %{opens_at: ~U[2022-01-20 12:00:00Z], period: %Postgrex.Range{lower: ~D[2022-01-10], upper: ~D[2022-02-01], lower_inclusive: true, upper_inclusive: true}, stay_policy_id: "2d4bb686-713f-4715-812f-6611dc2e2ba7"}, errors: [opens_at: {"is invalid", [constraint: :check, constraint_name: "stay_policy_periods_opens_before_start"]}], data: #ApiServer.V1.Stays.StayPolicyPeriod<>, valid?: false, ...>}}
[info] STAY POLICY PERIOD outer transaction result: {:error, :rollback}

For the Country test, which results in a non-database related validation error, I get what I need from the inner transaction (a changeset that I can process and return as an error to the client, with helpful info about which fields need to be corrected).

But for the StayPolicyPeriod test, I just get {:error, :rollback}.

My understanding is that this happens automatically because the constraint error happens on the database level, and even though it is “handled” by the check_constraint/3 function, it nevertheless causes a generic rollback to happen, because something went wrong at the database level.

Is this simply not solvable? Is there no way to get the changeset from the inner transaction instead of the generic rollback error without any information?

Marked As Solved

dimitarvp

dimitarvp

Welcome to the forum. :tada:

Few things come to mind:

  • Do you have actual DB CHECK CONSTRAINTs? They might be getting in the way.
  • Does the StayPolicyPeriod code touch a changeset where maybe the constraint itself is not mentioned and is only in the DB? You seem to imply that’s not the case, I would double check though (and I don’t have your context, hence the broad suggestion).
  • This might take you a bit of time but I would attempt to make a second variant of the code that utilizes Ecto.Multi and see if the error manifests the same way there.
  • I tried searching for {:error, :rollback}, even as a global GitHub search. Not many helpful leads but DBConnection’s docs have this to say about that return value:

All transaction/3 calls will return {:error, :rollback} if the transaction failed or connection closed and rollback/2 is not called for that transaction/3.

…which, does not help much, does it.

Interesting problem. Hope you keep us posted.

Also Liked

stiang

stiang

All transaction/3 calls will return {:error, :rollback} if the transaction failed or connection closed and rollback/2 is not called for that transaction/3 .

Actually, that does help, and although I had read that before I hadn’t read it carefully enough. If I do Repo.rollback(inner_transaction_result) instead of just returning inner_transaction_result, I get the following output:

[info] COUNTRY inner transaction result: {:ok, {:error, #Ecto.Changeset<action: :insert, changes: %{code: "TEST", enabled: true, name: "Test Country"}, errors: [phone_code: {"can't be blank", [validation: :required]}], data: #ApiServer.V1.Geo.Country<>, valid?: false, ...>}}
[info] COUNTRY outer transaction result: {:ok, {:ok, {:error, #Ecto.Changeset<action: :insert, changes: %{code: "TEST", enabled: true, name: "Test Country"}, errors: [phone_code: {"can't be blank", [validation: :required]}], data: #ApiServer.V1.Geo.Country<>, valid?: false, ...>}}}

[info] STAY POLICY PERIOD inner transaction result: {:ok, {:error, #Ecto.Changeset<action: :insert, changes: %{opens_at: ~U[2022-01-20 12:00:00Z], period: %Postgrex.Range{lower: ~D[2022-01-10], upper: ~D[2022-02-01], lower_inclusive: true, upper_inclusive: true}, stay_policy_id: "d0c4d95c-8cce-47ae-bd99-e42fdfca51c0"}, errors: [opens_at: {"is invalid", [constraint: :check, constraint_name: "stay_policy_periods_opens_before_start"]}], data: #ApiServer.V1.Stays.StayPolicyPeriod<>, valid?: false, ...>}}
[info] STAY POLICY PERIOD outer transaction result: {:error, {:ok, {:error, #Ecto.Changeset<action: :insert, changes: %{opens_at: ~U[2022-01-20 12:00:00Z], period: %Postgrex.Range{lower: ~D[2022-01-10], upper: ~D[2022-02-01], lower_inclusive: true, upper_inclusive: true}, stay_policy_id: "d0c4d95c-8cce-47ae-bd99-e42fdfca51c0"}, errors: [opens_at: {"is invalid", [constraint: :check, constraint_name: "stay_policy_periods_opens_before_start"]}], data: #ApiServer.V1.Stays.StayPolicyPeriod<>, valid?: false, ...>}}}

… which is exactly what I want. It shouldn’t require too much tweaking of the put function to make it do explicit rollbacks in such cases.

Thanks for pointing me in the right direction :smile:

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement