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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement