pinksynth

pinksynth

Does Ecto.Migration.modify/3 allow removal of default values?

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!

Most Liked

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.

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.

pinksynth

pinksynth

@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.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement