FreedomBen

FreedomBen

Hello, I am tearing my hair out trying to figure out why this doesn’t work. I am probably doing something dumb but I can’t figure it out. I’m very new to Elixir.

I’ve widdled my schema down to next to nothing to try and figure out what’s wrong. Here’s the migration:

defmodule TestApp.Repo.Migrations.CreateUsers do
  use Ecto.Migration

  def change do
    create table(:users, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :username, :string, null: false
    end 
  end 
end

And the schema file:

defmodule TestApp.Accounts.User do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  schema "users" do
    field :username, :string
  end   

  def changeset(user, attrs) do
    user
    |> cast(attrs, [:username])
  end   
end

And then some code to insert a test object:

TestApp.Accounts.User.changeset(%TestApp.Accounts.User{}, %{username: "ben"})
|> TestApp.Repo.insert()

Running that produces this:

** (Protocol.UndefinedError) protocol String.Chars not implemented for %Postgrex.Query{columns: nil, name: "", param_formats: nil, param_oids: nil, param_types: nil, ref: nil, result_formats: nil, result_oids: nil, result_types: nil, statement: ["INSERT INTO ", [34, "users", 34], [], [32, 40, [[], 34, "id", 34], ") VALUES ", [], 40, [[], 36 | "1"], 41], []], types: nil}. This protocol is implemented for: Atom, BitString, Date, DateTime, Ecto.Date, Ecto.DateTime, Ecto.Time, Float, Integer, List, NaiveDateTime, Time, URI, Version, Version.Requirement
    (elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
    (elixir) lib/string/chars.ex:22: String.Chars.to_string/1
    (ecto) lib/ecto/adapters/sql.ex:613: Ecto.Adapters.SQL.log/4
    (db_connection) lib/db_connection.ex:1186: DBConnection.log/6
    (db_connection) lib/db_connection.ex:591: DBConnection.prepare_execute/4
    (ecto) lib/ecto/adapters/postgres/connection.ex:86: Ecto.Adapters.Postgres.Connection.execute/4                                                                         
    (ecto) lib/ecto/adapters/sql.ex:256: Ecto.Adapters.SQL.sql_call/6
    (ecto) lib/ecto/adapters/sql.ex:542: Ecto.Adapters.SQL.struct/8
    (ecto) lib/ecto/repo/schema.ex:547: Ecto.Repo.Schema.apply/4
    (ecto) lib/ecto/repo/schema.ex:213: anonymous fn/14 in Ecto.Repo.Schema.do_insert/4                                                                                     
    (elixir) lib/code.ex:677: Code.require_file/2
    (mix) lib/mix/tasks/run.ex:136: Mix.Tasks.Run.run/5
    (mix) lib/mix/tasks/run.ex:76: Mix.Tasks.Run.run/1
    (mix) lib/mix/task.ex:314: Mix.Task.run_task/3
    (mix) lib/mix/cli.ex:80: Mix.CLI.run_task/2

I’ve done mix ecto.drop && mix ecto.create && mix ecto.migrate and all goes fine. I’ve also verified in iex that the changeset is valid. But when passing the changeset to Repo.insert it blows up with that error.

What am I doing wrong? What are some good ways to go about debugging this?

Showing Posts 1 to 10

idi527

idi527

:waving_hand:

Why are you passing :email to changeset when it’s :username that’s casted (and not null in the migration)?

FreedomBen

FreedomBen OP

Good catch, thanks, sorry that was a copy paste issue. I fixed the code above, and re-ran it locally and still get the same error.

script

script

try autogenerate: false in the schema

FreedomBen

FreedomBen OP

Thank you. I changed the schema file to:

defmodule TestApp.Accounts.User do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: false}
  schema "users" do
    field :username, :string
  end   

  def changeset(user, attrs) do
    user
    |> cast(attrs, [:username])
  end   
end

And got the same error message :frowning_face:

I looked through the Ecto migration docs and didn’t see anything similar for the migration file. Is there anywhere else autogenerate needs to be declared?

idi527

idi527

Where do you call

TestApp.Accounts.User.changeset(%TestApp.Accounts.User{}, %{username: "ben"})
|> TestApp.Repo.insert()

from?

FreedomBen

FreedomBen OP

I have this code in priv/repo/seeds.exs and run it with mix run priv/repo/seeds.exs :

TestApp.Accounts.User.changeset(%TestApp.Accounts.User{}, %{username: "ben"})
|> TestApp.Repo.insert()

I have also tried the same in an iex prompt started with iex -S mix with identical results. A unit test for the Accounts module also yields the same error.

idi527

idi527

Can you create a github repo with a way to reproduce the problem? It would help a lot since I don’t see anything wrong with your code snippets at all …

FreedomBen

FreedomBen OP

Yes, that is a great idea. I’ll also spin up a DO droplet and try to reproduce it there, just in case there’s something wonky with my local setup. I tried nuking the postgres instance and spinning up a fresh one, but this error occurs before postgrex even sends the query.

idi527

idi527

The problem seems to be with ecto/lib/ecto/adapters/sql.ex at v2.2.10 · elixir-ecto/ecto · GitHub, which means postgrex queries don’t conform to string.chars protocol for some reason which is strange since they should …

FreedomBen

FreedomBen OP

I am still waiting for permission from the client to post the entirety of the code, but I tried to reproduce the issue in a new project and have been unable to :exploding_head:

Where Next? Top

Trending in Questions Top

axelson
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
yasoob
Hi all! I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
neurodynamic
I have a page in my Phoenix app with some LiveView code. When you click a button, it shows the Stripe credit card modal form via a Hook. ...
New
Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
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...
261 13183 112
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews