zac
I’m having some trouble with EctoMigrationDefault – probably because I’m a novice with Elixir custom types and protocols, and I’m clearly getting something a bit wrong here.
10:09:57.706 [warning] You have specified a default value for a type that cannot be explicitly
converted to an Ecto default:
`[:value_add, :non_value_add]`
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.
This is coming from:
# activity_stereotype.ex
attributes do
attribute :allowed_types, {:array, COE.Types.ActivityTypes} do
allow_nil? false
default COE.Types.ActivityTypes.values
end
...
end
Trying to resolve this using EctoMigrationDefault I ended up here:
defmodule COE.Types.ActivityTypes do
@moduledoc ~S"""
Provides the allowed types for an `COE.Walk.ActivityStereotype`. Types are typically defined as either value add or non-value add.
"""
use Ash.Type.Enum, values: [:value_add, :non_value_add] # ~w(value_add non_value_add)a
@type t :: [:atom]
def to_string, do: inspect(Enum.map(values(), &(to_string(&1))))
end
defimpl EctoMigrationDefault, for: COE.Types.ActivityTypes do
def to_default(t), do: to_string(t)
end
Like I said, a novice with custom types (and protocols). With the above in place, I still get the warning about “a type that cannot be explicitly converted to an Ecto default.”
Footnote: I prefer the EctoMigrationDefault approach, but I did note that I can shut up the warning by adding migration_defaults [allowed_types: COE.Types.ActivityTypes.to_string] to the postgres block. But I don’t like how that creates an extra dependency on having a postgres block.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
Honestly, the
EctoMigrationDefaultisn’t the right answer here. You’d have to implement the protocol for arrays generically. We’re kind of moving away fromEctoMigrationDefaultin general anyway. I would do this:EDIT: sorry, just saw your footnote. I’d advise against using
EctoMigrationDefaultin general unless the value you’re using it for is a struct (likeMoney) and the type is not in your control (because if it is there is a callback you can use to set the default value).zac
Somewhere between ~2.x and 3.4, this stopped working. I’m trying to figure out the new and improved syntax for
migration_defaultswith an Ash.Enum. After upgrading, I’m getting:But of course… taking it out results in:
And I recall we had a previous conversation (above) about how
EctoMigrationDefaultisn’t really the right solution here.After futzing with it for some time (all afternoon!) I’ve given up and taken the default out. Just set it in code. But… sure would be nice to have it database enforced…
zachdaniel
What did you put in
migration_defaults?zac
It was originally what you had suggested above. As that seems to have led to problems I started digging into syntax changes, but haven’t found anything that worked. In the meantime, I’ve also realized there are some other, deeper problems (several tests failing), so I think the migration to 3.4 has not gone as well as I initially thought.
zac
Finally got around to solving this so I could shut up the build warning.
Ended up changing it to this:
The source attribute is:
And the referenced enum is:
(The point, of course, is to just consolidate the types in case additional types are added in the future, don’t want to have lots of
[:valid_add, :non_value_add]scattered throughout my code that I’d have to hunt down and fixup).Took a while to figure this out – until it occurred to me, just look in the database, see what is getting stuffed into the
default_typecolumn, and turn it into a literal string inmigration_defaults.Would be nice if
migration_defaults [allowed_types: COE.Walk.ActivityTypes]would work, but it doesn’t put a properly stringified value out… so it all blows up trying to run initial migrations with:I could probably add a
to_stringfunction… but posting this in case I’m missing something important here… plus, the required string isn’t really what I’d expect an enum to spit out “naturally” when converting to a string. Meh. Maybe call the functionto_migration_string…FYI @zachdaniel since this is a definite change from the old (~2.4 or whatever it was written on). Quite probably intentional, but it seems a bit less functional than the older implementation.