I’m in the process of updating a project from Elixir 1.13 to 1.16.
On the main branch all tests are passing, however on the 1.16 branch I’m getting a failed test:
** (RuntimeError) attempting to cast or change association `cart_items` from `MyApp.Carts.Cart` that was not loaded. Please preload your associations before manipulating them through changesets
code: assert should_add_shipping?(changeset)
stacktrace:
(ecto 3.10.3) lib/ecto/changeset/relation.ex:81: Ecto.Changeset.Relation.load!/2
(packsize_data 0.0.1) lib/myapp/carts/shipping.ex:112:
This is the test:
test "includes shipping", ctxt do
%{customer: customer, cart: cart} = ctxt
cart = %{cart | customer: customer}
changeset = change(cart, %{})
assert should_add_shipping?(changeset)
end
I get the error, it’s pretty straightforward, cart_items
is an association that’s not preloaded. The weird thing is that on the main branch calling changeset = change(cart, %{})
in the test returns a %Changeset{}
where cart_items
is already preloaded, however in my updated branch it is not. This happens in the test before any application code is called. Why would that be? I’m actually more confused that this was working before, I see why the test is failing now but how did it ever work‽
I realize without showing the entire codebase it may be difficult to diagnose this, there are very few changes other than updates to Elixir and some dependencies (see below) so I’m curious if something changed in a minor patch of Ecto or there is something obvious that change/2
preloads I’m missing.
Other relevant changes include:
Plug from 1.10.4 to 1.14.0
Plug Cowboy from 2.5.2 to 2.6
Ecto 3.10.2 to Ecto 3.10.3
Ecto SQL 3.10.1 to 3.10.2
Updates to Plug were specified in mix.exs
but Ecto was updated by running mix deps.update --all
.