Shenrak

Shenrak

Hi everyone,

I am trying to use Ecto.insert, and i have the following error :

[error] #PID<0.416.0> running GORprojectWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: POST /api/rooms
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in GORprojectWeb.ErrorHelpers.translate_error/1
        (gor_project) lib/gor_project_web/views/error_helpers.ex:9: GORprojectWeb.ErrorHelpers.translate_error("is invalid")
        (ecto) lib/ecto/changeset.ex:2345: anonymous fn/3 in Ecto.Changeset.merge_error_keys/3
        (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
        (ecto) lib/ecto/changeset.ex:2339: Ecto.Changeset.traverse_errors/2
        (gor_project) lib/gor_project_web/views/changeset_view.ex:17: GORprojectWeb.ChangesetView.render/2
        (phoenix) lib/phoenix/view.ex:332: Phoenix.View.render_to_iodata/3
        (phoenix) lib/phoenix/controller.ex:740: Phoenix.Controller.do_render/4
        (gor_project) lib/gor_project_web/controllers/room_controller.ex:1: GORprojectWeb.RoomController.action/2
        (gor_project) lib/gor_project_web/controllers/room_controller.ex:1: GORprojectWeb.RoomController.phoenix_controller_pipeline/2
        (gor_project) lib/gor_project_web/endpoint.ex:1: GORprojectWeb.Endpoint.instrument/4
        (phoenix) lib/phoenix/router.ex:278: Phoenix.Router.__call__/1
        (gor_project) lib/gor_project_web/endpoint.ex:1: GORprojectWeb.Endpoint.plug_builder_call/2
        (gor_project) lib/gor_project_web/endpoint.ex:1: GORprojectWeb.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:16: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /Users/giom/Documents/develop/web/GORProject-API/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

After some investigations, it seems that the error comes from an association not loaded, but the errors kind of triggers me because i have in my errors.pot the strings to handle this error :

## From Ecto.Changeset.put_change/3
msgid "is invalid"
msgstr ""

So i should be handled.

Here are my schemas, CRUD and controller :

Schema :

 defmodule GORproject.Rooms.Room do
  use Ecto.Schema
  import Ecto.Changeset

  alias GORproject.Rooms.Room
  alias GORproject.Auth.User

  schema "rooms" do
    field(:depth, :integer)
    field(:name, :string)
    field(:description, :string)
    field(:public, :boolean)
    many_to_many(:users, User, join_through: "users_rooms")
    has_many(:children, Room)
    belongs_to(:father, Room, foreign_key: :father_id)

    timestamps()
  end

  @doc false
  def changeset(room, attrs) do
    room
    |> cast(attrs, [:name, :depth, :description, :public, :father_id])
    |> foreign_key_constraint(:father_id)
    |> validate_required([:name, :depth, :public])
  end
end

CRUD :

  def create_room(attrs \\ %{}) do
    %Room{children: [], father: {}, users: []}
    |> Room.changeset(attrs)
    |> Repo.insert()
  end

Controller :

  def create(conn, %{"room" => room_params}) do
    with {:ok, %Room{} = room} <- Rooms.create_room(room_params) do
      conn
      |> put_status(:created)
      |> put_resp_header("location", room_path(conn, :create, room))
      |> render("show.json", room: room)
    end
  end

Thank you :slight_smile:

Showing Posts 18 to 9

Shenrak

Shenrak OP

The speed at which you’ve done this amazes me !
Thank you :slight_smile:

wojtekmach

wojtekmach

Hex Core Team

I’ve reproduced this in ecto test suite, I’ll report this upstream!

wojtekmach

wojtekmach

Hex Core Team

glad it’s fixed!

my ecto version is {:phoenix_ecto, "~> 3.2"}

could you paste your ecto version from mix.lock? (or run: mix deps | grep ecto :)) If you happen to run on a recent version there’s likely still a bug in ecto.

Shenrak

Shenrak OP

I got it working by puttin nil insteand of {}. Coming from javascript it’s still hard thinking in maps.
The error is sent by Ecto though, so i assume it’s an Ecto bug

Anyway, thank you both for your help :slight_smile:

ps : my ecto version is {:phoenix_ecto, "~> 3.2"}

wojtekmach

wojtekmach

Hex Core Team

btw, you’re defaulting father to tuple {}, you probably meant to write %{}, however for embeds/associations nil is probably better default. In any case, it’s still curious why you’re getting the error.

script

script

Thats why ecto throws an error. transalate error function expects a tuple.

wojtekmach

wojtekmach

Hex Core Team

errors: [father: "is invalid"]

I think this is the culprit, there errors should be in the form:

errors: [field: {message, metadata}]

so in this case:

errors: [father: {"is invalid", []}]

Are you setting the is invalid error yourself, or is Ecto setting it (via cast_assoc etc)? If the latter, it seems it’s an Ecto bug. What’s your Ecto version?

Shenrak

Shenrak OP

i’ve put two IO.inspect :

  def create_room(attrs \\ %{}) do
    %Room{children: [], father: {}, users: []}
    |> Room.changeset(attrs)
    |> IO.inspect()
    |> Repo.insert()
    |> IO.inspect()
  end

and i have :

[info] POST /api/rooms
[debug] Processing with GORprojectWeb.RoomController.create/2
  Parameters: %{"room" => %{"depth" => 1, "name" => "Accueillir", "public" => false}}
  Pipelines: [:api, :auth_api]
#Ecto.Changeset<
  action: nil,
  changes: %{depth: 1, name: "Accueillir", public: false},
  errors: [],
  data: #GORproject.Rooms.Room<>,
  valid?: true
>
[debug] QUERY OK db=0.3ms
begin []
[debug] QUERY OK db=0.2ms
rollback []
{:error,
 #Ecto.Changeset<
   action: :insert,
   changes: %{depth: 1, name: "Accueillir", public: false},
   errors: [father: "is invalid"],
   data: #GORproject.Rooms.Room<>,
   valid?: false
 >}

followed by the error stacktrace

script

script

Add IO.inspect after |> Room.changeset(attrs)

      def create_room(attrs \\ %{}) do
       %Room{children: [], father: {}, users: []}
       |> Room.changeset(attrs)
       |> IO.inspect()
       |> Repo.insert()
      end

And see what it gives you in the console. After you run this method

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
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews