Can't set a default value for an ecto string array (elixir list)

I’m working on a phoenix 1.3 project 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 posted this to stack overflow and got a great answer from dogbert. Here’s the link in case anyone finds it helpful!

3 Likes