【问题标题】:Ecto model `undefined function: ` when working with from macro ***in iex***Ecto模型`未定义的函数:`使用来自宏***在iex***中时
【发布时间】:2016-01-02 12:53:07
【问题描述】:

我在使用 Ecto 项目时遇到了这个问题。没有一个查询有效。 我做了一些谷歌搜索和 github 问题搜索。很少但与我的问题无关。

这个问题是从这个https://github.com/elixir-lang/ecto/issues/602#issuecomment-145596702开始的(主要和我的问题有关)

 query = from u in Univer, where: u.id > 4, select: u

** (RuntimeError) undefined function: u/0 炸毁。不仅是那个模型,其他模型也是如此。 我的部门。

  {:postgrex, "~> 0.9.1"},
  {:poison, "~> 1.5"},
  {:httpoison, "~> 0.7.2"},
  {:ecto, "~> 1.0.4"},
  {:floki, "~> 0.5"}

目前从 db 读取的所有内容都是通过 psql 完成的。它可以完成工作,但很烦人。 :)

供参考。

  defmodule Univer do
    use Ecto.Model

    import Ecto.Query

    schema "univers" do
      field :ref, :integer
      field :name, :string
      field :legal_name, :string
      field :city, :string
      field :type, :string
      field :address, :string
      field :contacts, {:array, :string}
      field :fax, :string
      field :phones, {:array, :string}
      field :email, :string
      field :url, :string
      has_many :schools, School
      has_one :place, Place
      timestamps
    end
  end

和迁移

  defmodule Univer.Repo.Migrations.AddUniversTable do
    use Ecto.Migration

    def up do
      create table(:univers) do
        add :ref, :integer
        add :name, :text
        add :legal_name, :text
        add :type, :string
        add :fax, :string
        add :city, :string
        add :contacts, {:array, :string}
        add :address, :text
        add :phones, {:array, :string}
        add :email, :string
        add :url, :string
        timestamps
      end
    end

    def down do
      drop table(:univers)
    end
  end

【问题讨论】:

    标签: elixir ecto


    【解决方案1】:

    我发现问题的核心是我对函数式语言中的古典语言魔法的期望。

    详细说明:

    如果您想在 IEX 控制台 (iex -S mix) 中测试查询。 你必须包括

    import Ecto.Query
    

    我将它包含在模块中,但没有包含在 IEX 控制台中。 我想这很愚蠢,但值得分享。

    【讨论】:

    • 这绝对是一个常见的陷阱!
    • 有没有一种简单的方法来预加载类似的东西?
    • @aramisbear 您可以在项目的根目录中添加一个.iex.exs 文件,其中包含import Ecto.Query。然后它会在你打开 IEX 时运行该命令。
    猜你喜欢
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多