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)
timestamps()
end
end
end
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
timestamps()
end
@doc false
def changeset(user_crypto, attrs) do
user_crypto
|> cast(attrs, [:user_id, :crypto_id])
|> validate_required([:user_id, :crypto_id])
|> foreign_key_constraint(:crypto_id)
|> foreign_key_constraint(:user_id)
end
end
The only change i did here is the id type