17

我在 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
4

1 回答 1

29

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

详细地:

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

import Ecto.Query

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

于 2015-10-05T19:07:30.407 回答