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 https://hexdocs.pm/ecto/Ecto.Schema.html

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

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
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
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
gazoon
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

Other popular topics Top

josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
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
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement