Search code examples
elixirecto

Does not recognize field in Ecto migration, even after flush


I'm following something similar to this answer, by adding a field and populating it in a migration.

def up do
  alter table(:posts) do
    add :urltitle, :string
  end

  flush()

  Repo.all(Post)
  |> Enum.map(fn p ->
       urltitle =
         p.title
         |> String.downcase
         |> Enum.map(fn c -> case URI.char_unreserved?(c) do
                               true -> c
                               false -> '-'
                             end
                     end)
         |> to_string
       Ecto.Changeset.cast(p, %{urltitle: urltitle}, ~w(urltitle))
     end)
  |> Repo.update_all()
end

With the flush(), it still has problems recognizing my new field :urltitle. The error message I get is

(ArgumentError) unknown field urltitle. Only fields, embeds and associations (except :through ones) are supported in changesets

What am I doing incorrectly?


Solution

  • This isn't a problem with the table not having the column. You forgot to add the field to your schema definition.