警告:函数Magnify.User .__ struct __ / 0未定义

时间:2018-04-14 01:41:12

标签: elixir phoenix-framework

我刚为我的凤凰项目安装了Coherence,但是当我运行本地服务器时,我收到以下警告消息,为什么会有任何想法。

更新了用户和模式,这是由Coherence创建的我没有创建这个,所有这些都在下面。

再次不确定导致这种情况,我明白Coherence会这样做。

enter image description here

warning: function Magnify.User.__struct__/0 is undefined (module Magnify.User is not available)
Found at 2 locations:
  lib/magnify/coherence/schemas.ex:35
  lib/magnify/coherence/schemas.ex:39

warning: function Magnify.User.changeset/2 is undefined (module Magnify.User is not available)
Found at 3 locations:
  lib/magnify/coherence/schemas.ex:31
  lib/magnify/coherence/schemas.ex:35
  lib/magnify/coherence/schemas.ex:39

schemas.ex

defmodule Magnify.Coherence.Schemas do

  use Coherence.Config

  import Ecto.Query

  @user_schema Config.user_schema
  @repo        Config.repo

  def list_user do
    @repo.all @user_schema
  end

  def get_by_user(opts) do
    @repo.get_by @user_schema, opts
  end

  def get_user(id) do
    @repo.get @user_schema, id
  end

  def get_user!(id) do
    @repo.get! @user_schema, id
  end

  def get_user_by_email(email) do
    @repo.get_by @user_schema, email: email
  end

  def change_user(struct, params) do
    @user_schema.changeset struct, params
  end

  def change_user(params) do
    @user_schema.changeset @user_schema.__struct__, params
  end

  def change_user do
    @user_schema.changeset @user_schema.__struct__, %{}
  end

  def update_user(user, params) do
    @repo.update change_user(user, params)
  end

  def create_user(params) do
    @repo.insert change_user(params)
  end

  Enum.each [Magnify.Coherence.Invitation], fn module ->

    name =
      module
      |> Module.split
      |> List.last
      |> String.downcase

    def unquote(String.to_atom("list_#{name}"))() do
      @repo.all unquote(module)
    end

    def unquote(String.to_atom("list_#{name}"))(%Ecto.Query{} = query) do
      @repo.all query
    end

    def unquote(String.to_atom("get_#{name}"))(id) do
      @repo.get unquote(module), id
    end

    def unquote(String.to_atom("get_#{name}!"))(id) do
      @repo.get! unquote(module), id
    end

    def unquote(String.to_atom("get_by_#{name}"))(opts) do
      @repo.get_by unquote(module), opts
    end

    def unquote(String.to_atom("change_#{name}"))(struct, params) do
      unquote(module).changeset(struct, params)
    end

    def unquote(String.to_atom("change_#{name}"))(params) do
      unquote(module).new_changeset(params)
    end

    def unquote(String.to_atom("change_#{name}"))() do
      unquote(module).new_changeset(%{})
    end

    def unquote(String.to_atom("create_#{name}"))(params) do
      @repo.insert unquote(module).new_changeset(params)
    end

    def unquote(String.to_atom("update_#{name}"))(struct, params) do
      @repo.update unquote(module).changeset(struct, params)
    end

    def unquote(String.to_atom("delete_#{name}"))(struct) do
      @repo.delete struct
    end
  end

  def query_by(schema, opts) do
    Enum.reduce opts, schema, fn {k, v}, query ->
      where(query, [b], field(b, ^k) == ^v)
    end
  end

  def delete_all(%Ecto.Query{} = query) do
    @repo.delete_all query
  end

  def delete_all(module) when is_atom(module) do
    @repo.delete_all module
  end

  def create(%Ecto.Changeset{} = changeset) do
    @repo.insert changeset
  end

  def create!(%Ecto.Changeset{} = changeset) do
    @repo.insert! changeset
  end

  def update(%Ecto.Changeset{} = changeset) do
    @repo.update changeset
  end

  def update!(%Ecto.Changeset{} = changeset) do
    @repo.update! changeset
  end

  def delete(schema) do
    @repo.delete schema
  end

  def delete!(schema) do
    @repo.delete! schema
  end

end

User.ex

defmodule Magnify.Coherence.User do
  @moduledoc false
  use Ecto.Schema
  use Coherence.Schema



  schema "users" do
    field :name, :string
    field :email, :string
    coherence_schema()

    timestamps()
  end

  def changeset(model, params \\ %{}) do
    model
    |> cast(params, [:name, :email] ++ coherence_fields())
    |> validate_required([:name, :email])
    |> validate_format(:email, ~r/@/)
    |> unique_constraint(:email)
    |> validate_coherence(params)
  end

  def changeset(model, params, :password) do
    model
    |> cast(params, ~w(password password_confirmation reset_password_token reset_password_sent_at))
    |> validate_coherence_password_reset(params)
  end
end

1 个答案:

答案 0 :(得分:0)

核心问题是它需要Magnify.User模块,但我的模块是Magnify.Coherence.User,如果你搜索Magnify.User,我会发现以下内容

config :coherence,
  user_schema: Magnify.User,
  repo: Magnify.Repo,

需要将user_schema:Magnify.User更改为Magnify.Coherence.User