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

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement