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
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 ![]()
/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
This won’t let id through, causing the built-in autogenerate to run instead.
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}
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








