cboebel
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!
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
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
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
This won’t let
idthrough, causing the built-inautogenerateto run instead.axelson
I don’t think that’s it since he’s passing the id as part of the struct itself
%ReplicationType{id: Ecto.UUID.generate(), name: "test3"}(and he’s not going through the changeset at all)@cboebel what version of mysql are you using and what is the exact column type of the id? Also you might want to checkout the MyXQL adapter: GitHub - elixir-ecto/myxql: MySQL 5.5+ driver for Elixir · GitHub (I don’t see any UUID support mentioned but I would it expect it to work with direct binaries)
cboebel
This is correct. The example I posted doesn’t use the changeset.
I’m using 5.7.25 MySQL Community Server (GPL).
The column type is binary(16)
cboebel
I should also point out that this happens if I generate the UUID myself or let it happen as part of the autogenerate.
axelson
What column type in MySQL are you using?
cboebel
binary(16)
cboebel
Also tried this we MySQL 8 - same results.
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}cboebel
MySQL does not like this:
isaac-rstor
Can you double check that your character encoding and collation is ok? Ecto’s MySQL doesn’t support certain of MySQL encoding types, and I remember having major problems with uuids that drove me up the wall (but were solvable - except when I had to interface with other databases at work) until I switched to postgres.
I can’t be 100% sure but I recall values being trimmed at 3f as being diagnostic of the problem – you’ll have to use utf8- something other other and not Latin, which is the default in MySQL iirc.