Ecto: ** (UndefinedFunctionError) undefined function: SomeModel.__schema__/1

Created on 12 Jan 2016  路  3Comments  路  Source: elixir-ecto/ecto

In the following code, the function find_all_channels fails with the error:

** (exit) an exception was raised:
    ** (UndefinedFunctionError) undefined function: Application.__schema__/1
        (elixir) Application.__schema__(:source)
        (ecto) lib/ecto/repo/queryable.ex:91: Ecto.Repo.Queryable.execute/5

defmodule Slacker.Application do
  use Slacker.Web, :model
  alias Slacker.Application
  alias Slacker.Settings
  alias Slacker.Repo
  alias Slacker.Channel

  @primary_key {:id, :string, []}
  @foreign_key_type :string
  @derive {Phoenix.Param, key: :id}

  schema "applications" do
    field :user_id, :integer
    field :name, :string

    has_one :settings, Settings
    has_many :channels, Channel

    timestamps
  end

  @required_fields ~w( name )
  @optional_fields ~w( user_id )

  def changeset(model, params \\ :empty) do
    model
    |> cast(params, @required_fields, @optional_fields)
  end

  def find_all_channels(application_id) do
    Repo.all(
      from c in Channel,
      join: a in assoc(c, :application),
      where: c.application_id == ^application_id)
  end

Most helpful comment

I had a issue in my Repo.insert , I defined as previously in User model
` schema "users" do

has_many(:authorize_tokens, AuthorizeToken)

timestamps()

end`

When I was calling this function
changeset = User.changeset(%User{}, user_params) case Repo.insert(changeset) do {:ok, _user} -> conn |> put_flash(:info, "User created successfully.") |> redirect(to: user_path(conn, :index)) {:error, changeset} -> render(conn, "new.html", changeset: changeset) end

It is producing following error

function AuthorizeToken.__schema__/1 is undefined (module AuthorizeToken is not available)

I only add one thing in my model

alias BlogTest.AuthorizeToken

Now everything is working fine

All 3 comments

That's because you are using Elixir's Application module instead of your Slacker.Application. My suggestion: call it something else to avoid confusion.

I had a issue in my Repo.insert , I defined as previously in User model
` schema "users" do

has_many(:authorize_tokens, AuthorizeToken)

timestamps()

end`

When I was calling this function
changeset = User.changeset(%User{}, user_params) case Repo.insert(changeset) do {:ok, _user} -> conn |> put_flash(:info, "User created successfully.") |> redirect(to: user_path(conn, :index)) {:error, changeset} -> render(conn, "new.html", changeset: changeset) end

It is producing following error

function AuthorizeToken.__schema__/1 is undefined (module AuthorizeToken is not available)

I only add one thing in my model

alias BlogTest.AuthorizeToken

Now everything is working fine

I just experienced this and I spent a little longer than I wanted to resolve it so here's some info, FWIW...

This can occur if there is a naming conflict between modules. e.g. you have a schema Foo and another module also named Foo.

Check if you have any naming conflicts!

:bowtie:

Was this page helpful?
0 / 5 - 0 ratings