Default value for {:array, :map} in migrations

1,239 views
Skip to first unread message

zipme

unread,
Aug 14, 2015, 3:18:06 AM8/14/15
to elixir-ecto
I am using `embeds_many` and the documentation says that "It is recommended to declare your embeds_many/3 field with type {:array, :map} and default value of [] at the database level."

So I try to do something like `add :items, {:array, :map}, default: []` in my migration.

However, when I run `mix ecto.migrate` I got the following error:
```
** (FunctionClauseError) no function clause matching in Ecto.Adapters.Postgres.Connection.default_expr/1
    (ecto) lib/ecto/adapters/postgres/connection.ex:597: Ecto.Adapters.Postgres.Connection.default_expr([])
    (ecto) lib/ecto/adapters/postgres/connection.ex:587: Ecto.Adapters.Postgres.Connection.column_options/1
    (ecto) lib/ecto/adapters/postgres/connection.ex:563: Ecto.Adapters.Postgres.Connection.column_change/1
    (elixir) lib/enum.ex:1008: anonymous fn/4 in Enum.map_join/3
    (elixir) lib/enum.ex:1261: Enum."-reduce/3-lists^foldl/2-0-"/3
    (elixir) lib/enum.ex:1007: Enum.map_join/3
    (ecto) lib/ecto/adapters/postgres/connection.ex:503: Ecto.Adapters.Postgres.Connection.execute_ddl/1
    (ecto) lib/ecto/adapters/postgres.ex:55: Ecto.Adapters.Postgres.execute_ddl/3
```

Any idea how I can specify the default as empty array for `{:array, :map}`? Do I need to use `fragment/1` for this? If so what should be the expression?

Thanks in advance.


zipme

unread,
Aug 14, 2015, 3:36:27 AM8/14/15
to elixir-ecto
Seems `add :items, {:array, :map}, default: fragment("ARRAY[]::jsonb[]")` works

在 2015年8月14日星期五 UTC+8下午3:18:06,zipme写道:

José Valim

unread,
Aug 15, 2015, 5:22:55 AM8/15/15
to elixi...@googlegroups.com
In master you will be able to do "default: []".



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/f2877495-596b-4f1b-afb9-ddfa85ec8ea9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages