stoic.alchemist

stoic.alchemist

Hey guys, I finally got the green light to upgrade our Phoenix/Elixir stack and I’m in the middle of updating what needs to be updated.

The issue I found is that moving from Ecto 3.3.3 to 3.10.1 (didn’t think it was a big jump without being a major version change when upgrading Phoenix) I see parts of the logic broken because the code expects Ecto.Repo.update!/2 to return the struct (as I see it says on the docs) but instead I see it returns an Ecto Changeset. I see this is expected on a bunch of places on the code so I’d like to know if there was a change in behavior and Changeset is the new return and the docs are outdated (which means I need to update a bunch of places in the code) or if this is a bug and I should report it and change to a lower version of Ecto?

Thanks in advance.

Showing Posts 1 to 9

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @stoic.alchemist can you show code that exhibits this behavior, and the IO.inspect output of the result? I have never seen this myself.

stoic.alchemist

stoic.alchemist OP

Sure thing, can’t show real code but I’ll emulate what’s happening:

iex> current_struct = %MyApp.MyModel{ key1: "value1"}
iex> updated_struct = 
         current_struct
         |> Ecto.Changeset.change(key: "value2")
         |> Ecto.Repo.update!
         |> IO.inspect

#Ecto.Changeset<
  action: nil,
  changes: %{key: "value2"},
  errors: [],
  data: #MyApp.MyModel<>,
  valid?: true
>

Please let me know if this is good enough, this is just a simplification of what’s happening, the real code goes around to a bunch of files (I’m also skipping the Model definition and such, I don’t think it’s important but I may be wrong)

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @stoic.alchemist is Ecto.Repo.update! a typo? That function is not callable directly. Not being able to show real code is definitely going to be a bit tricky.

If you can’t show your code, can you show an example that reproduces this behavior?

dimitarvp

dimitarvp

You are not supposed to call Ecto.Repo directly, you have to derive it via a macro in your own project. Then you use it like so: MyApp.Repo.update!.

stoic.alchemist

stoic.alchemist OP

Most definitely, we do have our own Repo definition, I’ll try to show as much as I can:
Repo definition:

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres
end

Model definition:

defmodule MyApp.MyModel do
  use Ecto.Schema
  import Ecto.Changeset
  alias MyApp.MyModel
  require Ecto.Query

  schema "my_models" do
    field :key, :string
    @timestamps_opts [type: :utc_datetime_usec]
    timestamps()
  end

  @fields [
    :key
  ]
 # A bunch of methods, around 200 lines or so
end

Now the Model it’s already initialized somewhere in the code:

# Somewhere inside a GenServer
my_model_struct
|> Ecto.Changeset.change(key: "value2")
|> MyApp.Repo.update!
|> IO.inspect() # Added to debug

This results in a:

#Ecto.Changeset<
  action: nil,
  changes: %{key: "value2"},
  errors: [],
  data: #MyApp.MyModel<>,
  valid?: true
>

Being that I’m doing the upgrade I thought this was a change in behavior but the docs say otherwise

stoic.alchemist

stoic.alchemist OP

:man_facepalming: Ok, I found the reason why… sorry for all the fuzz… tests are failing because the unit test file has a setup to use a “fake” Repo that returns the given data, which in this case is an Ecto.Changeset, so… sorry for the noise :sweat:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The docs indicate that this shouldn’t be possible, the return type of Ecto.Repo — Ecto v3.14.0 is Ecto.Schema.t(). Without code we can run this is pretty tricky to sort out, I’ve not seen anything like that before.

dimitarvp

dimitarvp

That’s a pretty weird and IMO unproductive test setup. I recommend you remove that fake repo.

stoic.alchemist

stoic.alchemist OP

Yes, I agree, I’ll figure out why that’s there and remove it if it’s not needed, this can hide bugs (or in this case waste time searching for not-bugs )

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews