AE
Ash Elixir•2y ago
moxley

cannot be explicitlyGetting error when generating migration: "...converted to an Ecto default"

I'm getting this error when trying to generate a migration:
16:56:34.288 [warning] You have specified a default value for a type that cannot be explicitly
converted to an Ecto default:

`[]`

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.
16:56:34.288 [warning] You have specified a default value for a type that cannot be explicitly
converted to an Ecto default:

`[]`

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.
I didn't have any new changes, and the error gave me no information about where in the codebase the problem came from. After some investigating and guessing, I discovered it was from this attribute:
attribute :attrs, {:array, EmbeddedWebComponent}, default: []
attribute :attrs, {:array, EmbeddedWebComponent}, default: []
First, I changed it to this:
attribute :attrs, {:array, EmbeddedWebComponent}
attribute :attrs, {:array, EmbeddedWebComponent}
Then, I wanted to follow the advice from the error, configure its default value in migration_defaults in the postgres section. The documentation wasn't clear on the syntax for the migration_defaults, so I guessed it should look like this:
# Make the default []
migration_defaults attrs: []
# Make the default []
migration_defaults attrs: []
After trying to generate the migration again, a new error appeared:
2 Replies
moxley
moxleyOP•2y ago
Generating Migrations:
Exception while formatting:

%SyntaxError{file: "nofile", line: 15, column: 1, snippet: nil, description: "syntax error before: end. \"end\" is a reserved word in Elixir and therefore its usage is limited. For instance, it can't be used as a variable or be defined nor invoked as a regular function"}

"defmodule GF.Repo.Migrations.SyncRemianing do\n @moduledoc \"\"\"\n Updates resources based on their most recent snapshots.\n\n This file was autogenerated with `mix ash_postgres.generate_migrations`\n \"\"\"\n\n use Ecto.Migration\n\n \n\n def up do\n alter table(:web_components) do\nmodify :attrs, {:array, :map}, default: \nend\n\n end\n\n def down do\n alter table(:web_components) do\nmodify :attrs, {:array, :map}, default: nil\nend\n\n end\nend\n"
Generating Migrations:
Exception while formatting:

%SyntaxError{file: "nofile", line: 15, column: 1, snippet: nil, description: "syntax error before: end. \"end\" is a reserved word in Elixir and therefore its usage is limited. For instance, it can't be used as a variable or be defined nor invoked as a regular function"}

"defmodule GF.Repo.Migrations.SyncRemianing do\n @moduledoc \"\"\"\n Updates resources based on their most recent snapshots.\n\n This file was autogenerated with `mix ash_postgres.generate_migrations`\n \"\"\"\n\n use Ecto.Migration\n\n \n\n def up do\n alter table(:web_components) do\nmodify :attrs, {:array, :map}, default: \nend\n\n end\n\n def down do\n alter table(:web_components) do\nmodify :attrs, {:array, :map}, default: nil\nend\n\n end\nend\n"
continued...
** (RuntimeError) Exception while formatting generated code:
** (SyntaxError) nofile:15:1: syntax error before: end. "end" is a reserved word in Elixir and therefore its usage is limited. For instance, it can't be used as a variable or be defined nor invoked as a regular function
(elixir 1.14.3) lib/code.ex:776: Code.format_string!/2
(ash_postgres 1.3.17) lib/migration_generator/migration_generator.ex:897: AshPostgres.MigrationGenerator.format/2
(ash_postgres 1.3.17) lib/migration_generator/migration_generator.ex:789: AshPostgres.MigrationGenerator.write_migration!/5
(elixir 1.14.3) lib/enum.ex:975: Enum."-each/2-lists^foreach/1-0-"/2
** (RuntimeError) Exception while formatting generated code:
** (SyntaxError) nofile:15:1: syntax error before: end. "end" is a reserved word in Elixir and therefore its usage is limited. For instance, it can't be used as a variable or be defined nor invoked as a regular function
(elixir 1.14.3) lib/code.ex:776: Code.format_string!/2
(ash_postgres 1.3.17) lib/migration_generator/migration_generator.ex:897: AshPostgres.MigrationGenerator.format/2
(ash_postgres 1.3.17) lib/migration_generator/migration_generator.ex:789: AshPostgres.MigrationGenerator.write_migration!/5
(elixir 1.14.3) lib/enum.ex:975: Enum."-each/2-lists^foreach/1-0-"/2
Okay, so solution is to use this syntax to define the default:
migration_defaults attrs: "[]"
migration_defaults attrs: "[]"
I didn't see documentation that described how to do that.
ZachDaniel
ZachDaniel•2y ago
🤔 that looks pretty strange ah, okay I see Yeah, we should make sure the values are strings could you make an issue for this on ash_postgres?

Did you find this page helpful?