I'm working on a phoenix 1.3 project (using the default postgres database) and I'm trying to add a string array column with a default value of a populated list.
Here's the migration:
defmodule DfyDashboard.Repo.Migrations.AddLeadSourceToOffice do
use Ecto.Migration
def change do
alter table(:offices) do
add :lead_sources, {:array, :string}, default: [
"Event",
"Facebook",
"Google/Online",
]
end
end
end
When I run the migration, I get the following error:
** (ArgumentError) unknown default
["Event", "Facebook", "Google/Online"]
for type{:array, :string}
. :default may be astring, number, boolean, empty list, map (when type is Map), or a fragment(...)
It seems like it isn't possible to have anything but an empty list for a default when using {:array, input_type}
.
Is what I'm trying to do with the default actually a hack and there's a better way to do it? Or if what I'm trying to do is the preferred method, is there a hack I can use to do the same thing? Any help is appreciated.
I'm not sure why Ecto only supports empty arrays as default but you can use fragment
for now and specify the array in PostgreSQL syntax:
add :lead_sources, {:array, :string},
default: fragment("ARRAY['Event', 'Facebook', 'Google/Online']")
Support for empty arrays was added in 2015 but not non-empty arrays. I think they would be open to adding this feature. You might want to ask the core devs on their mailing list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With