【发布时间】:2021-06-10 09:26:05
【问题描述】:
与这个问题类似:Using a Repo in an Ecto migration,我有一个 Ecto 迁移,我想在其中创建一些新表,但也从 不同的 存储库迁移一些数据。例如:
defmodule MyApp.Repo.Migrations.CreateFoo do
use Ecto.Migration
import Ecto.Query
def change do
create table(:foo) do
add(:status, :text)
end
flush()
execute &import_from_AnotherApp/0
end
defp import_from_AnotherApp()
foos = from(f in AnotherApp.Foo, where: ...)
|> AnotherApp.Repo.all
Enum.each(foos, fn(foo) ->
# Insert data from AnotherApp into MyApp foo table
end)
end
end
问题是在运行 mix ecto.migrate 时我得到了
** (RuntimeError) could not lookup Ecto repo AnotherApp.Repo because it was not started or it does not exist
我尝试在mix.exs 中添加AnotherApp 作为MyApp 的依赖项,但我仍然遇到同样的错误。
这个可以吗?
【问题讨论】:
-
您是否尝试过在执行查询之前手动启动
AnotherApp.Repo? ecto 迁移混合任务未启动您的完整应用程序。 -
@joshnuss 是的。我确实喜欢 Everett 的回答,而且效果很好
标签: elixir database-migration ecto