DEV Community

mix ecto.migrate で重複エラーが起きた時のメモ

すいませんものすごく個人的なメモです。

問題

mix ecto.migrateを行った際以下のようなエラーが発生

$ mix ecto.migrate
warning: found quoted keyword "test" but the quotes are not required. Note that keywords are always atoms, even when quoted. Similar to atoms, keywords made exclusively of Unicode letters, numbers, underscore, and @ do not require quotes 
  mix.exs:56

** (Ecto.MigrationError) migrations can't be executed, migration name create_articles is duplicated
    (ecto) lib/ecto/migrator.ex:275: Ecto.Migrator.ensure_no_duplication/1
    (ecto) lib/ecto/migrator.ex:256: Ecto.Migrator.migrate/4
    (ecto) lib/mix/tasks/ecto.migrate.ex:83: anonymous fn/4 in Mix.Tasks.Ecto.Migrate.run/2
    (elixir) lib/enum.ex:783: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:783: Enum.each/2
    (mix) lib/mix/task.ex:331: Mix.Task.run_task/3
    (mix) lib/mix/cli.ex:79: Mix.CLI.run_task/2
    (elixir) lib/code.ex:813: Code.require_file/2

英文ではcreate_articles名が重複しとるぞと

解決法(とりあえず解決法を知りたい人に)

テーブル名が同じ名前のためエラーが出たのこと、
以下のコマンドで通ります。
mix ecto.migrate --step 1

原因

どうやら migrate する際にテーブル名が重複していたようでmigrationを一つに実行すると通るというわけですね。

You can fix it by running 1 migration at a time

mix ecto.migrate --step 1 こちらのコマンドをうつと

$ mix ecto.migrate --step 1
warning: found quoted keyword "test" but the quotes are not required. Note that keywords are always atoms, even when quoted. Similar to atoms, keywords made exclusively of Unicode letters, numbers, underscore, and @ do not require quotes 
  mix.exs:56

[info] == Running App.Repo.Migrations.CreateArticles.change/0 forward
[info] create table articles
[info] == Migrated in 0.1s

やったぜ実行できました!

Ideally you should not have 2 migrations with the same name :)

以下の記事でもあるようにテーブル名は同じ名前じゃだめだよーとのこと
むむーどこで作成しちまっていたのか...
メモでした。

参考リンク

How to fix Ecto duplicate name migrations error
http://disq.us/t/2jnipyj
ありがとう https://github.com/minhajuddin さん

そのほか日本語での mix tasks を解説してくださっている記事です。
Phoenix入門 (第14章 Mix Tasks その2) - 技術メモ
https://www.tech-note.info/entry/phoenix-14-mix-tasks-2#mix-ectogenmigration
ありがたいありがたい

mix tasks は一通り覚えようと思いました...

Latest comments (0)