简体   繁体   English

Elixir 应用程序在 iex 中工作,但在尝试混合运行时失败

[英]Elixir app working in iex but fail when try to mix run

I am trying to create application with Elixir, Ecto without Pheonix.我正在尝试使用没有 Pheonix 的 Elixir、Ecto 创建应用程序。

mix.exs 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

repo.ex回购协议

defmodule Wtf.Repo do
  use Ecto.Repo,
    otp_app: :wtf,
    adapter: Ecto.Adapters.Postgres
end

application.ex应用程序.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

config.exs配置文件

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 wtf.ex

defmodule Wtf do
  import Ecto.Query, warn: false

  alias Wtf.{Repo, Data}

  def hello do
    Repo.all(WtfData)
  end
end

Below I am attaching list of my files in project like you can see that is just basic setup for quering database.下面我附上了我在项目中的文件列表,就像您看到的那样,这只是查询数据库的基本设置。 When I run iex -S mix app loading properly and I can query database all CRUD working fine, when I do Wtf.Repo.all(Wtf.Data) I am getting all 3 rwos which I have in database.当我运行iex -S mix应用程序加载正确并且我可以查询数据库所有 CRUD 工作正常时,当我执行Wtf.Repo.all(Wtf.Data)时,我得到了数据库中的所有 3 个 rwo。 But when I am trying to run this app with mix run lib/wtf.ex I am getting error function Repo.all/1 is undefined (module Repo is not abvailable) I was looking in the google how to solve it and it seems like I am doing everything like it should be done.但是当我尝试使用mix run lib/wtf.ex运行这个应用程序时,我得到了错误function Repo.all/1 is undefined (module Repo is not abvailable)我在谷歌中寻找如何解决它,看起来像我正在做所有应该做的事情。 I follow few examples but always the same issue.我遵循几个例子,但总是同样的问题。 Can some explain me what is wrong with it?有人可以解释一下它有什么问题吗? What am I missing?我错过了什么?

Sidenote: of course you don't need to build a release unless you want to run a release.旁注:当然,除非您想运行发布,否则您不需要构建发布。

The below part of mix.exs defines the application within your project and its entry point. mix.exs的以下部分定义了项目中的应用程序及其入口点。

def application do
  [
    extra_applications: [:logger, :postgrex, :ecto, :ecto_sql],
#   ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓ THIS
    mod: {Wtf.Application, []}
  ]
end

Instead of running an explicit file with mix run ‹file› , do mix run without a parameter, which is, according to docs,不要使用mix run ‹file›运行显式文件,而是在没有参数的情况下进行mix run ,根据文档,

can be used to start the current application dependencies, the application itself, and optionally run some code in its context.可用于启动当前应用程序依赖项、应用程序本身,并可选择在其上下文中运行一些代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行mix ecto.setup时,Elixir Postgres错误42501? - Elixir Postgres ERROR 42501 when running mix ecto.setup? Elixir Phoenix ecto.create 失败 - Elixir Phoenix ecto.create fail 获取绑定消息提供 2 个参数,但是当尝试从 nodejs 应用程序运行 postgresql 查询时,准备好的语句“”需要 0 个错误 - getting bind message supplies 2 parameters, but prepared statement “” requires 0 error when try to run postgresql query from nodejs app Elixir-Phoenix:混合 ecto.create 和混合 phx.server 会引发相同的错误,但包含不同的 postgres db 参数 - Elixir-Phoenix: mix ecto.create and mix phx.server provoke same error but contain different postgres db parameters 当我尝试运行heroku run rake db:migrate时发生Postgres错误 - Postgres error when I try to run heroku run rake db:migrate 尝试运行 Rails 应用程序时出现超时错误 - Timeout error when trying to run a Rails app 当我尝试运行以下查询时,出现错误子查询必须只返回一列: - I am getting the error subquery must return only one column when I try to run the following query: Phoenix Framework:为什么混合 ecto.create 和混合 phx.server 不起作用? - Phoenix Framework: why mix ecto.create and mix phx.server not working? 设置事务隔离级别时的Phoenix / Elixir测试 - Phoenix / Elixir testing when setting isolation level of transaction 如何在 elixir 的 Ecto Queries 中使用“case-when”? - How to use "case-when" in Ecto Queries in elixir?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM