2015-10-05 5 views
16

私はEctoプロジェクトでこの問題が発生しています。クエリのいずれも機能していません。 私はグーグルとgithub問題の検索の公正を少し公平した。私の問題はほとんどありませんが、無関係です。Ectoモデル `未定義関数:`マクロから作業するとき*** iex ***

この質問は** (RuntimeError) undefined function: u/0を吹く

query = from u in Univer, where: u.id > 4, select: u 
(私の問題に主に関連する) https://github.com/elixir-lang/ecto/issues/602#issuecomment-145596702この1から開幕しました。そのモデルだけでなく、他のモデルも同様です。 私のdeps。現在、すべてのDBからの読み取りが psqlを介して行われる

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

。仕事はしても迷惑です。 :)

参考までに。

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)でクエリをテストする場合。 私はモジュールではなく、IEXコンソールでそれを含めた

import Ecto.Query 

あなたが含まれている必要があります。 それはかなりばかだが分かち合う価値があると思う。

+4

これは間違いなく一般的な落とし穴です! –

+0

そのようなものをプリロードする簡単な方法はありますか? – brightball

+5

@aramisbear 'import Ecto.Query'を含むプロジェクトのルートに' .iex.exs'ファイルを追加することができます。 IEXを開くと、そのコマンドが実行されます。 –

関連する問題