Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ecto model `undefined function: ` when working with from macro ***in iex***

Tags:

elixir

ecto

I am having this issue with Ecto project. None of the queries are working. I did fair a bit of googling and github issues search. There are few but unrelated to my problem.

This question was kicked off from this one https://github.com/elixir-lang/ecto/issues/602#issuecomment-145596702 (mostly related to my problem)

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

Blows up with ** (RuntimeError) undefined function: u/0. Not only that model but other models as well. My deps.

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

Currently all reading from db is done via psql. It does the job but annoying. :)

For the reference.

  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

and the migration

  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
like image 896
Andrew Shatnyy Avatar asked Oct 05 '15 18:10

Andrew Shatnyy


1 Answers

I found the core of the problem is my expectation of Classical language magic in Functional language.

In detail:

If you want to test out queries in IEX console (iex -S mix). You must include

import Ecto.Query

I was including it in the module but not in IEX console. It's pretty stupid but worth sharing, I suppose.

like image 176
Andrew Shatnyy Avatar answered Nov 04 '22 18:11

Andrew Shatnyy