![](/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.