fireproofsocks

fireproofsocks

Defining Ecto Schemas (PostGres) to use NOT NULL in column definitions

I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Consider the following schema:

schema "addresses" do
    field :name, :string, size: 100
    field :company, :string, size: 100
    field :street1, :string, size: 250
    field :street2, :string, size: 250
    field :city, :string, size: 100
    field :state, :string, size: 2
    field :zip, :string, size: 32
    field :zip4, :string, size: 4  # NOT NULL?
    timestamps()
  end

I would prefer that the zip4 column can never be null… is there a way to do this? I don’t see it referenced on Ecto.Schema — Ecto v3.14.0

Thanks!

Most Liked

OvermindDL1

OvermindDL1

You don’t specify if a column can be null or not in the schema, you do it in the migration (by setting , null: false). :slight_smile:

Remember, Ecto does not duplicate checks that the database already does (as many are just impossible to know ahead of time, although this one could be, it’s good to keep consistency).

14
Post #2
jeremyjh

jeremyjh

This is correct but it is possible to enforce this constraint in your changeset function, which will yield a nice Changeset error rather than an exception.You can use validate_required in your changeset function. I’d still set null: false in the migration.

10
Post #3
jeremyjh

jeremyjh

So far as I know, the “right” way to setup the changeset to make a field be required is to use the Changeset function validate_required. That is how we do it. If I take the validate_required function out of my Changeset and leave the field blank, I end up with a Postgrex exception along the lines:

** (exit) an exception was raised:
    ** (Postgrex.Error) ERROR 23502 (not_null_violation): null value in column "title" violates not-null constraint

    table: listings
    column: title

I don’t have any code I can share at the moment.

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
Tee
can someone please explain to me how Enum.reduce works with maps
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
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

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement