Pow Assent invalid association `user_identities`

Hello I am using Pow Assent to setup Github oauth for my project. However I’m running into the following error:

warning: invalid association `user_identities` in schema Profilo.Accounts.Lib.User: associated schema Profi
lo.Accounts.UserIdentities.UserIdentity does not exist
  lib/profilo/accounts/lib/user.ex:1: Profilo.Accounts.Lib.User (module)

user_identitiy.ex

defmodule Profilo.Accounts.Lib.UserIdentities.UserIdentity do
  use Ecto.Schema
  use PowAssent.Ecto.UserIdentities.Schema, user: Profilo.Accounts.Lib.User

  schema "user_identities" do
    pow_assent_user_identity_fields()

    timestamps(updated_at: false)
  end
end

user.ex

defmodule Profilo.Accounts.Lib.User do
  use Ecto.Schema
  use Pow.Ecto.Schema
  use PowAssent.Ecto.Schema

  import Ecto.Changeset

  alias Bcrypt

  schema "users" do
    field :first_name, :string
    field :last_name, :string
    field :address, :string
    field :company, :string
    field :phone_number, :string
    field :website, :string
    field :is_admin, :boolean, default: false

    pow_user_fields()

    timestamps()
  end

  @required_fields ~w(first_name last_name)a
  @optional_fields ~w(address company phone_number website)a

  def changeset(user, attrs) do
    user
    |> pow_changeset(attrs)
    |> cast(attrs, @required_fields ++ @optional_fields)
    |> validate_required(@required_fields)
  end
end

I feel like the error has to with my project structure. Although I am not sure how to change PowAssent config for user_identity.

2 Likes

@danschultzer What do you think?

It’s because you using the Lib namespace. PowAssent can only guess where the UserIdentity module is, but you can override this by adding a has_many association macro:

defmodule Profilo.Accounts.Lib.User do
  # ...

  schema "users" do
    has_many :user_identities,
      Profilo.Accounts.Lib.UserIdentities.UserIdentity,
      on_delete: :delete_all,
      foreign_key: :user_id

    field :first_name, :string
    field :last_name, :string
    field :address, :string
    field :company, :string
    field :phone_number, :string
    field :website, :string
    field :is_admin, :boolean, default: false

    pow_user_fields()

    timestamps()
  end

  # ...
end

This way you can also rename the user identity module to Profilo.Accounts.Lib.UserIdentity so it conforms to the naming of your User module.

Edit: I’ve updated the readme: https://github.com/danschultzer/pow_assent#different-module-naming

3 Likes