Unknown field for changeset?

711 views
Skip to first unread message

Luke Gordon

unread,
May 28, 2017, 8:15:26 PM5/28/17
to elixir-ecto
I've been banging my head against the following for a few hours, any help would be greatly appreciated!

I'm getting an "unknown field" error when trying to insert an ecto model.. but from everything I can see the field is there and is initialized.  Any thoughts?  
I've attached the error stack trace and model below.  As you can see, it's logging out the "provider" field and it's value, but it looks like it's not getting cast correctly??

Error Stack Trace:
[info] Initializing: %{external_id: "555555555555555", provider: "google", user_id: "f349cfb0-c082-46b0-83da-a8c42c294627"}
[debug] QUERY OK db=0.1ms
rollback []
[info] Sent 500 in 601ms
[error] #PID<0.521.0> running Innovate.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /auth/google/callback?code=4/EV6yEMpPA248M-z78Lew0FPguewD6WTS6CQPgZn9L7E
** (exit) an exception was raised:
    ** (ArgumentError) unknown field "provider" for changeset on %AuthService.OAuthCredentials{__meta__: #Ecto.Schema.Metadata<:built, "oauth_credentials">, created_at: nil, external_id: nil, id: nil, provider: nil, updated_at: nil, user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: nil}
        (ecto) lib/ecto/changeset.ex:1318: Ecto.Changeset.ensure_field_exists!/2
        (ecto) lib/ecto/changeset.ex:1305: anonymous fn/5 in Ecto.Changeset.validate_required/3
        (elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
        (ecto) lib/ecto/changeset.ex:1304: Ecto.Changeset.validate_required/3
        (auth_service) lib/auth_service/oauth_registration.ex:12: anonymous fn/2 in AuthService.OAuthRegistration.create/2
        (ecto) lib/ecto/multi.ex:406: Ecto.Multi.apply_operation/5
        (elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
        (ecto) lib/ecto/multi.ex:396: anonymous fn/5 in Ecto.Multi.apply_operations/5
        (ecto) lib/ecto/adapters/sql.ex:620: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
        (db_connection) lib/db_connection.ex:1275: DBConnection.transaction_run/4
        (db_connection) lib/db_connection.ex:1199: DBConnection.run_begin/3
        (db_connection) lib/db_connection.ex:790: DBConnection.transaction/3
        (ecto) lib/ecto/repo/queryable.ex:21: Ecto.Repo.Queryable.transaction/4
        (auth_service) lib/auth_service/oauth_registration.ex:16: AuthService.OAuthRegistration.create/2
        (innovate) web/controllers/auth_controller.ex:15: Innovate.AuthController.callback/2
        (innovate) web/controllers/auth_controller.ex:1: Innovate.AuthController.action/2
        (innovate) web/controllers/auth_controller.ex:1: Innovate.AuthController.phoenix_controller_pipeline/2
        (innovate) lib/innovate/endpoint.ex:1: Innovate.Endpoint.instrument/4
        (innovate) lib/phoenix/router.ex:261: Innovate.Router.dispatch/2
        (innovate) web/router.ex:1: Innovate.Router.do_call/2 

And this is the model: 
defmodule AuthService.OAuthCredentials do
  require Logger
  use Ecto.Schema
  import Ecto.Changeset 
  @primary_key {:id, :binary_id, read_after_write: true} 
  schema "oauth_credentials" do
    belongs_to :user, AuthService.User
    field :provider,    :string
    field :external_id, :string
    field :created_at,  :naive_datetime
    field :updated_at,  :naive_datetime
  end 
  @required ~w(provider external_id user_id) 
  def build(params \\ %{}) do
    changeset(%AuthService.OAuthCredentials{}, params)
  end 
  def changeset(model, params \\ %{}) do
    Logger.info("Initializing: #{inspect params}")
    model
    |> cast(params, [:provider, :external_id, :user_id])
    |> validate_required(@required)
  end
end

Ben Wilson

unread,
May 28, 2017, 9:45:43 PM5/28/17
to elixir-ecto
Pretty sure your required list needs to be a list of atoms, not a list of strings. You want `  @required ~w(provider external_id user_id)a`. Note the trailing `a`.

Luke Gordon

unread,
May 29, 2017, 1:04:21 AM5/29/17
to elixir-ecto
You're absolutely right.  Thanks for the fast responses guys!  In one day I've gone from not knowing Elixir to having a multi-auth umbrella project rolling.  Thanks for all of the quick feedback!!!

Ben Wilson

unread,
May 29, 2017, 12:32:00 PM5/29/17
to elixir-ecto
Glad to hear it! I highly suggest checking out https://elixirforum.com/ as well, it tends to get more activity these days.
Reply all
Reply to author
Forward
0 new messages