How to set null to empty string array in ecto migration

Hey I have a column defined as such:

add(:variations, {:array, :string})

I want to run a migration to set every null to an empty list, but it seems troublesome.
I got it finally by doing this:

    from(
      from(bai in "business_accounts_items",
        where: is_nil(bai.variations),
        update: [set: [variations: fragment("ARRAY[]::text[]")]]
      )
    )
    |> Repo.update_all([])

Is this the way to do it properly? specifically the

update: [set: [variations: fragment(“ARRAY[]::text[]”)]]

part

Thanks!

1 Like

I would say yes. I mean, does work. Also make sure that you have a default value in your database field so you only have to do this once.

2 Likes