执行mix ecto.migrate遇MatchError:找不到postgrex.app文件或目录
解决
mix ecto.migrate中的postgrex.app找不到错误 问题场景
作为Elixir新手,执行mix ecto.migrate时遇到以下错误:
** (MatchError) no match of right hand side value: {:error, {:postgrex, {'no such file or directory', 'postgrex.app'}}} (ecto_sql 3.8.3) lib/ecto/migrator.ex:136: Ecto.Migrator.with_repo/3 (ecto_sql 3.8.3) lib/mix/tasks/ecto.migrate.ex:141: anonymous fn/5 in Mix.Tasks.Ecto.Migrate.run/2 (elixir 1.13.4) lib/enum.ex:2396: Enum."-reduce/3-lists^foldl/2-0-"/3 (ecto_sql 3.8.3) lib/mix/tasks/ecto.migrate.ex:129: Mix.Tasks.Ecto.Migrate.run/2 (mix 1.13.4) lib/mix/task.ex:397: anonymous fn/3 in Mix.Task.run_task/3 (mix 1.13.4) lib/mix/cli.ex:84: Mix.CLI.run_task/2
相关配置文件、Repo模块及迁移文件代码如下:
配置文件
use Mix.Config config :zeri, ecto_repos: [Zeri.Repo] database_url = "postgresql://postgres:9835674725@localhost:5432/kaisa_db" config :zeri, Zeri.Repo, url: database_url
Repo模块
defmodule Zeri.Repo do use Ecto.Repo, otp_app: :zeri, adapter: Ecto.Adapters.Postgres end
迁移文件
defmodule Zeri.Repo.Migrations.First do use Ecto.Migration def change do execute("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";", "") create(table(:users, primary_key: false)) add :id, :uuid, primary_key: true, default: fragment("uuid_generate_v4()") add :username, :text, null: true end end end
解决方案
这个错误的核心是Postgrex依赖未安装或未正确编译,Postgrex是Ecto连接PostgreSQL的底层驱动,必须确保它被添加到项目依赖中并完成安装。
- 检查并添加Postgrex依赖
打开项目根目录下的mix.exs文件,找到deps函数,确保包含:postgrex依赖:
defp deps do [ {:ecto_sql, "~> 3.8"}, {:postgrex, ">= 0.0.0"} ] end
版本号可根据你的ecto_sql版本调整,保持兼容即可。
- 下载并编译依赖
在终端执行以下命令:
mix deps.get mix deps.compile
- 重新执行迁移
完成依赖安装后,再次运行:
mix ecto.migrate
额外检查点
- 如果执行
mix deps.get后仍报错,可尝试删除deps目录和mix.lock文件,重新执行mix deps.get - 确保本地已安装PostgreSQL服务,且配置文件中的数据库连接信息正确
内容的提问来源于stack exchange,提问作者WICKED
相关产品推荐
相关产品推荐

