anelixiruser

anelixiruser

When I add a column to an existing table, I’d like to set a default value for the rows already in the table, but I don’t want there to be a default value for any newly added rows. I also want it to be a not-null column.

The problem is that if I don’t set a default, the not null constraint gets violated and the migration fails. What I’m looking for is a simple way to specify a default for existing rows, while still causing any future inserts to fail if a value is not specified for that column.

This feels like a relatively common situation, and it would be nice not to have to break things out and write up() and down() functions and instead just use a change() and specify a “default for existing rows”.

An explicit mechanism to either specify a value or call a function to fill in data prior to proceeding with setting the column not null would seem like a natural way to solve this. I haven’t found any indication this exists, but the situation I am running into seems like it would be a fairly common problem.

Here’s an example of an approach I sometimes use. It feels like way too much code for what I’m wanting to achieve.

defmodule Rtphx.Repo.Migrations.ExampleAddColumn do
  use Ecto.Migration

  def up do
    alter table(:example) do
      add :newcolumn, :string, default: "defaultvalue", null: false
    end
    alter table(:example) do
      modify :newcolumn, :string, default: nil, null: false
    end
  end

  def down do
    alter table(:example) do
      remove :newcolumn,  :string, null: false
    end
  end

end

Showing Posts 1 to 4

kip

kip

ex_cldr Core Team

An Ecto migration using the builtin macros is pretty much a 1:1 mapping to the databases underlying DML so I think your code is exactly as much as it needs to be.

dimitarvp

dimitarvp

  1. Have a default value in your migration.
  2. Make your Ecto schema module validate the new field/column as mandatory so Ecto will never issue an SQL INSERT / UPDATE without it, thus never using the default value specified in the database.
anelixiruser

anelixiruser OP

I guess I’m spoiled by being able to just write a “change” function most of the time, where Ecto figures out the reverse migration for me.

chartrand22

chartrand22

You could have used this :

  def change do
    alter table(:example) do
      add :newcolumn, :string, null: false
    end
    execute "update example set newcolumn = whatever_you_want"
  end```
— 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
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews