cboebel

cboebel

Ecto (mysql) and UUID

Hello,

I’m having a vexing issue using Ecto.UUIDs as my primary key in a table. When I create a record, I generate the UUID for the id, and pass that along with the other fields to insert. A record is created, and the UUID is shown. When I query the record, I get a different UUID back.

Here’s an example of what I’m seeing. Code for the schema and repo follow the example.

I believe I’m doing something dumb but, at least for me, the dumber it is the harder it is to find.

iex(5)> Repo.insert(%ReplicationType{id: Ecto.UUID.generate(), name: "test3"})
18:32:49.063 [debug] QUERY OK db=0.8ms queue=7.6ms
INSERT INTO `replication_types` (`id`,`name`,`inserted_at`,`updated_at`) VALUES (?,?,?,?) [<<71, 34, 96, 242, 205, 97, 72, 47, 171, 47, 132, 161, 18, 187, 187, 138>>, "test3", ~N[2019-04-19 22:32:49], ~N[2019-04-19 22:32:49]]
{:ok,
 %Pserver.Pserver.ReplicationType{
   __meta__: #Ecto.Schema.Metadata<:loaded, "replication_types">,
   id: "472260f2-cd61-482f-ab2f-84a112bbbb8a",
   inserted_at: ~N[2019-04-19 22:32:49],
   name: "test3",
   replication_tables: #Ecto.Association.NotLoaded<association :replication_tables is not loaded>,
   sites: #Ecto.Association.NotLoaded<association :sites is not loaded>,
   updated_at: ~N[2019-04-19 22:32:49]
 }}
iex(6)> Repo.get_by(ReplicationType, name: "test3")                           
18:33:11.506 [debug] QUERY OK source="replication_types" db=8.9ms
SELECT r0.`id`, r0.`name`, r0.`inserted_at`, r0.`updated_at` FROM `replication_types` AS r0 WHERE (r0.`name` = ?) ["test3"]
%Pserver.Pserver.ReplicationType{
  __meta__: #Ecto.Schema.Metadata<:loaded, "replication_types">,
  id: "4722603f-3f61-482f-3f2f-3f3f123f3f3f",
  inserted_at: ~N[2019-04-19 22:32:49],
  name: "test3",
  replication_tables: #Ecto.Association.NotLoaded<association :replication_tables is not loaded>,
  sites: #Ecto.Association.NotLoaded<association :sites is not loaded>,
  updated_at: ~N[2019-04-19 22:32:49]
}

the code for ReplicationType:

defmodule Pserver.Pserver.ReplicationType do
  use Ecto.Schema
  import Ecto.Changeset
  alias Pserver.Pserver.{ReplicationTable, Site}

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "replication_types" do
    # field :id, :binary_id
    field :name, :string
    
    has_many :sites, Site
    has_many :replication_tables, ReplicationTable
    timestamps()
  end

  @doc false
  def changeset(replication_type, attrs) do
    replication_type
    |> cast(attrs, [:name])
    |> validate_required([:name])
    |> unique_constraint(:name)
  end
end

And the Repo…

defmodule Pserver.Repo do
  use Ecto.Repo,
    otp_app: :pserver,
    adapter: Ecto.Adapters.MySQL
end

Thank you!

Most Liked

Rainer

Rainer

I updated my test to output the uuid’s:
insert: a0569da7-4201-47a8-bab0-4aca263b735a => ok
insert: cc2d9293-5898-4118-8352-c192090bf6ef => fail, got new id: 3f2d3f3f-583f-4118-3f52-3f3f090b3f00

another run:
insert: 2361f715-48f9-4574-9cb2-379883a8adc4 => got new id: 23613f15-483f-4574-3f3f-373f3f3f3f00

interesting: the new id’s are sharing some parts with the original ones…

/e: now found this: binary id odd bug · Issue #2602 · elixir-ecto/ecto · GitHub
i’ll try that later, hope it fixes the problem :slight_smile:

/e²: Fixed it by adding utf8mb4 to the repo config (config/dev.exs):

# Configure your database
config :testproject, Testproject.Repo,
  username: "xyz",
  password: "xyz",
  database: "xyz",
  hostname: "xyz",
  charset: "utf8mb4",
  collate: "utf8mb4_unicode_ci"
al2o3cr

al2o3cr

This won’t let id through, causing the built-in autogenerate to run instead.

simon

simon

I discovered this in the context of migrations so it might not make any difference here but when your define your primary key add read_after_writes: true.

@primary_key {:id, :binary_id, autogenerate: true, read_after_writes: true}

Where Next?

Trending in Questions Top

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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view ...
New
cgraham
Hi! What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
stefanchrobot
Hi, I need a way to handle data migrations in my application. I found an article by @wojtekmach about manual migrations: Automatic and ma...
New
stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
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
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
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
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement