![](/img/trans.png)
[英]Elixir Postgres ERROR 42501 when running mix ecto.setup?
[英]Elixir app working in iex but fail when try to mix run
我正在尝试使用没有 Pheonix 的 Elixir、Ecto 创建应用程序。
mix.exs
defmodule Wtf.MixProject do
use Mix.Project
def project do
[
app: :wtf,
version: "0.1.0",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger, :postgrex, :ecto, :ecto_sql],
mod: {Wtf.Application, []}
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:postgrex, "~> 0.16.3"},
{:ecto, "~> 3.8.4"},
{:ecto_sql, "~> 3.8.3"},
{:json, "~> 1.4"},
{:jason, "~> 1.3.0"},
{:ratatouille, "~> 0.5.0"},
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
回购协议
defmodule Wtf.Repo do
use Ecto.Repo,
otp_app: :wtf,
adapter: Ecto.Adapters.Postgres
end
应用程序.ex
defmodule Wtf.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
@impl true
def start(_type, _args) do
children = [
# Starts a worker by calling: Wtf.Worker.start_link(arg)
# {Wtf.Worker, arg}
Wtf.Repo
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Wtf.Supervisor]
Supervisor.start_link(children, opts)
end
end
配置文件
import Config
config :wtf, Wtf.Repo,
database: "wtf_database",
username: "wtf",
password: "wtf",
hostname: "localhost",
port: "5050",
log: false
config :wtf, ecto_repos: [Wtf.Repo]
wtf.ex
defmodule Wtf do
import Ecto.Query, warn: false
alias Wtf.{Repo, Data}
def hello do
Repo.all(WtfData)
end
end
下面我附上了我在项目中的文件列表,就像您看到的那样,这只是查询数据库的基本设置。 当我运行iex -S mix
应用程序加载正确并且我可以查询数据库所有 CRUD 工作正常时,当我执行Wtf.Repo.all(Wtf.Data)
时,我得到了数据库中的所有 3 个 rwo。 但是当我尝试使用mix run lib/wtf.ex
运行这个应用程序时,我得到了错误function Repo.all/1 is undefined (module Repo is not abvailable)
我在谷歌中寻找如何解决它,看起来像我正在做所有应该做的事情。 我遵循几个例子,但总是同样的问题。 有人可以解释一下它有什么问题吗? 我错过了什么?
旁注:当然,除非您想运行发布,否则您不需要构建发布。
mix.exs
的以下部分定义了项目中的应用程序及其入口点。
def application do
[
extra_applications: [:logger, :postgrex, :ecto, :ecto_sql],
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓ THIS
mod: {Wtf.Application, []}
]
end
不要使用mix run ‹file›
运行显式文件,而是在没有参数的情况下进行mix run
,根据文档,
可用于启动当前应用程序依赖项、应用程序本身,并可选择在其上下文中运行一些代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.