pinksynth

pinksynth

Hi all! First time posting, let me know if I can structure my question better.

I am using Ecto migrations on a project and need to add columns with non-NULL constraints to a table with existing data. My down simply removes the columns. My up adds the columns, uses default values to get placeholder data in the DB, flushes, and then uses Ecto.Migration.modify/3 to add the non-NULL constraints and remove the defaults. However, when I look at the table description in Postgres, I see not null default NULL::character varying. It looks like the database still behaves properly, but shouldn’t this modifier be gone entirely?

Here’s my migration’s up:

  def up do
    # Step 1. Add location fields with default values
    alter table(:campuses) do
      add :street_1, :string, default: "__INVALID_STREET"
      add :street_2, :string
      add :city, :string, default: "__INVALID_CITY"
      add :state, :string, size: 2, default: "ZZ"
      add :zip, :string, size: 16, default: "__INVALID_CITY"
      add :lon, :float, default: 0.0
      add :lat, :float, default: 0.0
    end

    # Step 2. Execute above commands.
    flush()

    # Step 3. Remove default option and add null constraint
    alter table(:campuses) do
      modify :street_1, :string, null: false, default: nil
      modify :street_2, :string
      modify :city, :string, null: false, default: nil
      modify :state, :string, size: 2, null: false, default: nil
      modify :zip, :string, size: 16, null: false, default: nil
      modify :lon, :float, null: false, default: nil
      modify :lat, :float, null: false, default: nil
    end
  end

Thanks all!

Showing Posts 1 to 6

dimitarvp

dimitarvp

Have you tried simply removing the default: nil options and re-run the migration?

florish

florish

@pinksynth It’s been a while, but did you find a solution for this?

joddm

joddm

From Postgres documentation

To remove any default value, use:

ALTER TABLE products ALTER COLUMN price DROP DEFAULT;

This is effectively the same as setting the default to null. As a consequence, it is not an error to drop a default where one hadn’t been defined, because the default is implicitly the null value.

pinksynth

pinksynth OP

@florish Thanks for asking! It has been a long time, I do not recall what the solution was. In retrospect I embarrassingly don’t really understand the problem either :sweat_smile:. Perhaps there is some difference between “No default” and “Default: null”, but I don’t think that difference had any practical impact on the project or code. Thanks @joddm for the docs reference.

florish

florish

Yes, well, that’s a good question indeed. Out of curiosity, I’ve checked the PostgreSQL documentation, and it’s mostly a difference between an implicit NULL (which is the default if no DEFAULT is set in PostgreSQL) and an explicit NULL default value:

DEFAULT default_expr

The DEFAULT clause assigns a default data value for the column whose column definition it appears within. (…)

If there is no default for a column, then the default is null.

So practically no impact, the only difference is the presence / absence of an expliciet DEFAULT NULL statement in your CREATE TABLE SQL code.

dimitarvp

dimitarvp

I can confirm that this worked for me in a migration. I needed to change the type of a column so I did the dance with making a new column, migrating the data from the old one to the new one but the new one had to allow a default value because the migration was multi-step and then when everything was done I made a migration to delete the old column and to remove the default value from the new column while still disallowing NULL / nil.

— 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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
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
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
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews