With this Ecto migration:
defmodule Migrations.Foo do
use Ecto.Migration
def change do
create table(:foo, primary_key: false) do
add(:id, :uuid, primary_key: true)
add(:bars, :map, null: false)
end
end
end
I can’t get empty lists to be saved in the database.
This doesn’t work:
%Foo{bars: []} |> Repo.insert
# produces:
# ** (Postgrex.Error) ERROR 23502 (not_null_violation) null value in column "bars" violates not-null constraint
But this does work:
%Foo{bars: [%{foo: "bar"}]} |> Repo.insert
I tried to change my migration type to {:array, :map}
but it doesn’t work either.
I can get around this by allowing null
values and setting a default, like that add(:bars, :map, default: "[]")
but it doesn’t seem right.
What am I missing?