Ash FrameworkAF
Ash Framework3y ago
5 replies
Blibs

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


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.
Was this page helpful?