References on UUID issue: is invalid

Hello, I work on a project where i need a relation many to many between 2 tables, i have a schema between them witch includes id’s of both but when inserting a record on this table I have some trouble :

If i use default id (1, 2, 3…) i have no problems but as soon as i implement uuid instead of id, on insertion of the row i get this problem :

    "errors": {
        "crypto_id": [
            "is invalid"
        "user_id": [
            "is invalid"

Here is the migration file for the relation table :

defmodule CryptoApi.Repo.Migrations.CreateUsersCryptos do
  use Ecto.Migration

  def change do
    create table(:users_cryptos, primary_key: false) do
      add :id, :uuid, primary_key: true
      add :user_id, references(:users, type: :uuid, on_delete: :delete_all)
      add :crypto_id, references(:cryptos, type: :uuid, on_delete: :delete_all)

And here is the schema of this table :

defmodule CryptoApi.Accounts.UserCrypto do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, Ecto.UUID, autogenerate: true}

  schema "users_cryptos" do
    belongs_to :user, Accounts.User
    belongs_to :crypto, Currencies.Crypto


  @doc false
  def changeset(user_crypto, attrs) do
    |> cast(attrs, [:user_id, :crypto_id])
    |> validate_required([:user_id, :crypto_id])
    |> foreign_key_constraint(:crypto_id)
    |> foreign_key_constraint(:user_id)

The only change i did here is the id type

I think you need to add @foreign_key_type :binary_id to your schema.

On a somewhat related note, I migrated my project from using UUIDs back to normal bigint, and transforming the ids when interacting with web or other interfaces. It’s something you may consider.

The main drawback of working with UUIDs, is that it makes life much harder when debugging your data.