2015-10-05 40 views
16

我與Ecto項目有此問題。沒有任何查詢正在工作。 我做了一些谷歌搜索和github問題搜索。有幾個,但與我的問題無關。Ecto model`未定義的函數:`當從宏***在iex中工作***

這個問題是從這一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"} 

當前所有的數據讀取都是通過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 

回答

26

我發現這個問題的核心是我在函數式語言古典語言魔力的期望。

詳細:

如果你想測試出IEX控制檯(iex -S mix)查詢。 您必須包括

import Ecto.Query 

我包括它的模塊中,但不是在IEX控制檯。 這很愚蠢,但值得分享,我想。

+4

這絕對是一個常見的陷阱! –

+0

有沒有簡單的方法來預加載這樣的東西? – brightball

+5

@aramisbear您可以在包含'import Ecto.Query'的項目根目錄中添加一個'.iex.exs'文件。當您打開IEX時,它會運行該命令。 –