azrael
Issues with unique_constraint and many to many
Hi,
I am working on a first elixir project and I am struggling quite a lot with the many_to_many association. I have got it sort-of working using the auto-generated link table, however it does not seem to be checking for uniqueness on the link succesfully.
defmodule KevinCadleFantasyGame.Lineup do
use Ecto.Schema
import Ecto.Changeset
alias KevinCadleFantasyGame.{Player, Repo, Lineup}
schema "lineups" do
many_to_many :players, Player,
join_through: "lineup_players", unique: true
timestamps()
end
@doc false
def changeset(lineup, attrs) do
player_ids = if Map.has_key?(attrs, :player_ids), do: attrs.player_ids, else: []
players = Player.by_ids(player_ids)
lineup
|> Repo.preload(:players)
|> cast(attrs, [:id])
|> put_assoc(:players, players)
|> unique_constraint(:players)
|> validate_required([])
end
def create_or_update(params) do
cs = changeset(%Lineup{}, params)
if cs.valid? do
Repo.insert!(cs,
on_conflict: :nothing,
conflict_target: :id
)
else
cs
end
end
end
and the migration:
defmodule KevinCadleFantasyGame.Repo.Migrations.CreateLineups do
use Ecto.Migration
def change do
create table(:lineups) do
timestamps()
end
create table("lineup_players", primary_key: false) do
add :player_id, references(:players)
add :lineup_id, references(:lineups)
end
create unique_index(:lineup_players, [:player_id, :lineup_id])
end
end
With a non-unique link it is giving me a postgres error due to a failure of uniqueness constraint, which is fine, but ideally I’d probably like it to be caught on the changset. What am i doing wrong?
Thanks,
Az
First Post!
kokolegorille
Hello and welcome, You might give a name to your constraint…
create unique_index(:lineup_players, [:player_id, :lineup_id], name: :my_constraint)
and use it like this
|> unique_constraint(:players, name: :my_constraint)
Popular in Questions
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it.
I’m very interested in Elixir,...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
I have a list say
x = ["23gh", "56kh", "97mh"]
I would like to pass each element to Val in each iteration.
Say, in iteration 1 -------...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
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
Other popular topics
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New







