I’m able to create and migrate my database tables Ok. But when I try to seed it, I’m getting errors as soon as I encounter a table that uses the timestamps()
macro in its migration definition:
** (Postgrex.Error) ERROR 23502 (not_null_violation) null value in column "inserted_at" violates not-null constraint
table: tenants
column: inserted_at
My migration looks like this:
defmodule Auth.Repo.Migrations.Tenants do
use Ecto.Migration
def change do
create table(:tenants, primary_key: false) do
add :id, :string, size: 16, null: false, primary_key: true # Slug
add :status_id, references("tenants_statuses", type: :string), size: 10, null: false
add :name, :string, size: 32, null: false, default: ""
add :description, :text, null: false, default: ""
timestamps()
end
end
end
And my seeds.exs
like this:
alias Auth.Repo
Repo.insert_all(
"tenants_statuses",
[
%{id: "on", name: "On", description: "Signups and logins for this tenant are enabled"},
%{id: "off", name: "Off", description: "Signups and logins for this are disabled"},
]
)
Repo.insert_all(
Auth.Schemas.Tenant,
[
%{
id: "wholesale",
name: "Wholesale",
status_id: "on",
description: "Deny Designs and the Wholesale channel",
}
]
)
I’ve tried using both the string table name as the 1st argument or the schema module, but the error is the same. When I inspect the table inside PostGreSQL, I see that it has not defined a default value:
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
-------------+--------------------------------+-----------+----------+-----------------------+----------+--------------+-------------
id | character varying(16) | | not null | | extended | |
status_id | character varying(10) | | not null | | extended | |
name | character varying(32) | | not null | ''::character varying | extended | |
description | text | | not null | ''::text | extended | |
inserted_at | timestamp(0) without time zone | | not null | | plain | |
updated_at | timestamp(0) without time zone | | not null | | plain | |
I thought that using timestamps()
would set up a default value for both of those columns, but clearly I’m missing something. Can anyone point out what I’m missing? Thanks!