Pending migration error - try running `mix ecto.migrate` (which does not help)

Hello, on any app I have been working on the last couple of days it has been giving me this error when I go to run an app with a module.

# there are pending migrations for repo: LiveViewTodos.Repo. Try running `mix ecto.migrate` in the command line to migrate it

running the mix ecto.migrate does not do anything. Here is an example of something simple that I have done in my todo_live.ex file.

defmodule LiveViewTodosWeb.TodoLive do
  use LiveViewTodosWeb, :live_view

  def mount(_params, _session, socket) do
    {:ok, socket}
  end

  def render(assigns) do
    ~L"Rendering LiveView"
  end
end

Any suggestions on what is going wrong?

If it is in dev, try to run…

mix ecto.reset

It will reset your db. Beware if You need to keep your data!

Try to wrap your code with triple ` , as in Markdown. It will be more readable :slight_smile:

3 Likes

Perhaps running

mix ecto.migrations

gives you a hint of the missing migrations

1 Like

Thank you. I did what you had said running mix ecto.migrations. Do you know how I would add the missing file?

kimberlybrooke@Kimberlys-MacBook-Pro live_view_todos % mix ecto.migrations

Repo: LiveViewTodos.Repo

Status Migration ID Migration Name

up 20210714024300 ** FILE NOT FOUND **
down 20210720024531 create_todos

kimberlybrooke@Kimberlys-MacBook-Pro live_view_todos %

Thank you and okay! next time I will wrap my code in the triple `

Thank you!

1 Like

Hi Kimberly,

There is a table created automatically by Ecto called schema_migrations. This table contains a list of all the migrations that are already applied to the database. Each row corresponds to a migration file that starts with a timestamp.

In your case, it seems that you have one entry in your schema_migrations table:

  • 20210714024300

However, in the corresponding folder at priv/repo/migrations you have this file:

  • 20210720024531_create_todos

As you can realize, the timestamps in the table and in the files do not correspond.

20210714024300 != 20210720024531

Somehow, the file that starts with 20210714024300 is missing in your folder.

Warning, the following solution is not common, and it should not be used unless you know the risks. Normally, data in schema_migrations table is handled automatically by Ecto.

If you are sure that you do not need the missing migration file, you can remove by hand the corresponding entry in the schema_migrations table. After that, running mix ecto.migrations should not show any messages.

As it seems that you are running some experiments, another option is delete the database, recreate it and run the migrations again with the following command.

mix ecto.drop
mix ecto.create
mix ecto.migrate

Warning mix ecto.drop destroys your development database.

Hope this helps.

3 Likes

Thank you!

mix ecto.reset is an alias :slight_smile:

  defp aliases do
    [
      setup: ["deps.get", "ecto.setup", "cmd npm install --prefix assets"],
      "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
      "ecto.reset": ["ecto.drop", "ecto.setup"],
      test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"]
    ]
  end

it was only resolved by deleting the _build folder

rm -rf _build/

For the future generations to come, a note:

Deleting the _build/dev/lib/<your_app_name> & rebuilding seems to be enough to convince phoenix_ecto that migrations are not pending.