Default value for {:array, :atom} attribute

Hello, I'm trying to have a default value when using an {:array, :atom} attribute like this:
attribute :roles, {:array, :atom} do
allow_nil? false

default [:seller]

constraints [
items: [one_of: [:seller, :admin]],
min_length: 1,
nil_items?: false
]
end
attribute :roles, {:array, :atom} do
allow_nil? false

default [:seller]

constraints [
items: [one_of: [:seller, :admin]],
min_length: 1,
nil_items?: false
]
end
But when I try to persist a new instance of this resource it fails because :roles is empty. Also, when generating a migration I get this error:
11:18:42.224 [warning] You have specified a default value for a type that cannot be explicitly
converted to an Ecto default:

`[:seller]`

The default value in the migration will be set to `nil` and you can edit
your migration accordingly.

To prevent this warning, implement the `EctoMigrationDefault` protocol
for the appropriate Elixir type in your Ash project, or configure its
default value in `migration_defaults` in the postgres section. Use `\"nil\"`
for no default.
11:18:42.224 [warning] You have specified a default value for a type that cannot be explicitly
converted to an Ecto default:

`[:seller]`

The default value in the migration will be set to `nil` and you can edit
your migration accordingly.

To prevent this warning, implement the `EctoMigrationDefault` protocol
for the appropriate Elixir type in your Ash project, or configure its
default value in `migration_defaults` in the postgres section. Use `\"nil\"`
for no default.
5 Replies
Blibs
BlibsOP3y ago
Ah... ops, the first part of my question is not true, I forgot to run the migration so it was failing because of that and not because the roles field was empty... But that warning is still there when generating the migration, is it safe to just ignore it?
frankdugan3
frankdugan33y ago
That warning is just letting you know that you have to manually edit the migration to set that default for Postgres. You specified it correctly, it's just a case that the migration generator doesn't know how to automatically handle.
Blibs
BlibsOP3y ago
So, I edited the migration file and added the default value, but the warning still shows up, can I stop it from showing after the default value is added to the migration file somehow?
ZachDaniel
ZachDaniel3y ago
You don’t do it by editing the migration, you specify in the resource
postgres do
migration_defaults roles: “[]”
end
postgres do
migration_defaults roles: “[]”
end
Blibs
BlibsOP3y ago
Thank you, that fixed it!

Did you find this page helpful?