azrael

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

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)

Where Next?

Popular in Questions Top

Brian
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
LegitStack
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
nsuchy
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
lk-geimfari
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
mathew4509
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
belgoros
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
Codball
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
lanycrost
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 Top

New
script
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
minhajuddin
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
ashish173
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
johnnyicon
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
magnetic
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

We're in Beta

About us Mission Statement