.__struct__/1 is undefined, cannot expand struct

I am building a Phoenix 1.3 app but following a 1.2 Pheonix tutorial, I ran deprecated commands like mix phoenix.gen.model

I am not sure if it is linked but now I get this error:

== Compilation error in file lib/gazette_web/controllers/user_controller.ex ==
** (CompileError) lib/gazette_web/controllers/user_controller.ex:12: Gazette.User.__struct__/1 is undefined, cannot expand struct Gazette.User
    lib/gazette_web/controllers/user_controller.ex:11: (module)
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

Here is my user_controller.ex file:

defmodule Gazette.UserController do
  use GazetteWeb, :controller

  alias Gazette.User

  def show(conn, %{"id" => id}) do
    user = Repo.get!(User,id)
    render(conn, "show.html", user: user)
  end

  def new(conn, _params) do
    changeset = User.changeset(%User{})
    render conn, "new.html", changeset: changeset
  end

  def create(conn, %{"user" => user_params}) do
    # here will be an implementation
  end
end

and this is my model/user.ex file:

defmodule Gazette.User do
  use Gazette.Web, :model

  schema "users" do
    field :email, :string
    field :name, :string
    field :password, :string, virtual: true
    field :password_hash, :string
    field :is_admin, :string
    field :is_writer, :string

    has_many :posts, Gazette.Post

    timestamps()
  end

  @required_fields ~w(email name)a
  @optional_fields ~w(is_admin is_writer)a

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, @required_fields ++ @optional_fields)
    |> validate_required(@required_fields)
  end
end

any idea ?

:wave: @JulienCorb

use Gazette.Web, :model this might be the problem. Check if there is model function defined in Gazette.Web (web.ex or gazette_web.ex or something like that).

Maybe replacing use Gazette.Web, :model with use Gazette.Web, :schema might work.

Phoenix (pre 1.3) used to have some things named differently.

Hey @idi527 thank you for your help !

Just discovered that Gazette.Web is deprecated; that was the problem indeed.
I tried :schema but it did not work;
I added use Ecto.Schema import Ecto.Changeset alias Gazette.User but I ran into some new errors.

As I was at the very beggining of my project, I simply restarted from scratch and used the right mix command to create my tables in my DB.

Maybe you can make the project in 1.2.0 and after that use this to upgrade the project to 1.3.0: https://www.phoenixdiff.org/?source=1.2.0&target=1.3.0

1 Like

Thank you mihai ! I managed to do the 1.2 tutorial and adapting it for 1.3 myself. Actually a good way to learn !

2 Likes

If that helped you, then you are the first one I ever helped :slight_smile: Super day!

3 Likes