axelclark

axelclark

How would I write a migration to add the following unique constraint to my database?

Given the table below, check that when status == "active", the combination of fantasy_team_id and position are unique. In other words, a fantasy team can’t have more than one of any position if the status is “active”.

table: 
roster_positions

column_names:
fantasy_team_id
position
status

From the Ecto Migration docs
https://hexdocs.pm/ecto/Ecto.Migration.html#constraint/3
I went to the PostresQL docs

I got to the code below, but I’m not sure exactly how to write the migrations since it looks like constraint/3 is used for CHECK. Also, I’m not sure how to apply the constraint only when the status is “active”.

ALTER TABLE roster_positions (
    UNIQUE (fantasy_team_id, position)
);

Does anyone have any tips or recommended resources to research this some more? Thanks!
Axel

Showing Posts 1 to 7

OvermindDL1

OvermindDL1

So to add a unique constraint you put in something like create unique_index(:roster_positions, [:position]), but since you want a constraint with it, specifically to only have a unique index on parts where status is active then that would be added in a where clause, such as in create unique_index(:roster_positions, [:position], where: "status LIKE 'active'") or something like that, thus only rows where status is like “active” (or whatever constraint you want) will be included in this unique index, all others are ignored and will not have this unique constraint. :slight_smile:

axelclark

axelclark OP

Thanks, I’ll give this a try!

OvermindDL1

OvermindDL1

And for note, if you want literally only the combination of the :fantasy_team_id and the :position to be unique and not only individual, then in my above example instead of [:position] you would have [:fantasy_team_id, :position] instead, or whatever combinations you want. :slight_smile:

axelclark

axelclark OP

Thanks! Once I started implementing it, I realized I also needed to exclude rows in which position is ‘Unassigned’ which is the default value. Here is what I finally used:

  def up do
    create unique_index(:roster_positions, [:position, :fantasy_team_id],
      where: "status LIKE 'active' AND position != 'Unassigned'")
  end

  def down do
    drop unique_index(:roster_positions, [:position, :fantasy_team_id],
      where: "status LIKE 'active' AND position != 'Unassigned'")
  end

Thanks for the help!!

mgwidmann

mgwidmann

Wouldn’t it be better to do an exact comparison on status? If that field only has active/inactive states I suggest using a boolean, but if its more than that I suggest using ecto_enum as it will allow you to operate w/ obvious values like :active/:pending/:inactive but uses integers in the database. More efficient for the DB and cleaner data.

axelclark

axelclark OP

@mgwidmann, thanks for the suggestion. I hadn’t seen ecto_enum, but I’ll check it out

OvermindDL1

OvermindDL1

On using ecto_enum and if you are using PostgreSQL, I highly recommend setting up PostgreSQL enum type in the database itself, helps you to pack the data even tighter and makes dumps readable! :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews