Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem updating changesets with relations. #17

Closed
itSQualL opened this issue May 4, 2017 · 4 comments
Closed

Problem updating changesets with relations. #17

itSQualL opened this issue May 4, 2017 · 4 comments

Comments

@itSQualL
Copy link

itSQualL commented May 4, 2017

I'm trying to use PaperTrail.update to update a record that has a has_many relation, but it is triggering the next error:

web_1  | #Ecto.Changeset<action: nil,                                                                                                          [4/4594]
web_1  |  changes: %{current_status: 49,
web_1  |    key_results: [#Ecto.Changeset<action: :update,
web_1  |      changes: %{values: %{"current_value" => 49, "desired_value" => 100}},
web_1  |      errors: [], data: #Performancex.KeyResult<>, valid?: true>]}, errors: [],
web_1  |  data: #Performancex.Objective<>, valid?: true>
web_1  | [debug] QUERY OK db=1.3ms
web_1  | begin []
web_1  | [debug] QUERY OK db=2.2ms
web_1  | UPDATE "objectives" SET "current_status" = $1, "updated_at" = $2 WHERE "id" = $3 [49, {{2017, 5, 4}, {10, 40, 57, 718198}}, 1]
web_1  | [debug] QUERY OK db=0.4ms
web_1  | UPDATE "key_results" SET "values" = $1, "updated_at" = $2 WHERE "id" = $3 [%{"current_value" => 49, "desired_value" => 100}, {{2017, 5, 4}, {1
0, 40, 57, 720656}}, 1]
web_1  | [debug] QUERY ERROR db=5.3ms
web_1  | INSERT INTO "versions" ("event","item_changes","item_id","item_type","originator_id","inserted_at") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "id",
 "origin" ["update", %{current_status: 49, key_results: [#Ecto.Changeset<action: :update, changes: %{values: %{"current_value" => 49, "desired_value" =
> 100}}, errors: [], data: #Performancex.KeyResult<>, valid?: true>]}, 1, "Objective", 2, {{2017, 5, 4}, {10, 40, 57, 721334}}]
web_1  | [debug] QUERY OK db=0.1ms
web_1  | rollback []
web_1  | [info] Sent 500 in 53ms
web_1  | [error] #PID<0.644.0> running Performancex.Endpoint terminated
web_1  | Server: localhost:4000 (http)
web_1  | Request: PUT /api/v1/performance/objectives/1
web_1  | ** (exit) an exception was raised:
web_1  |     ** (Poison.EncodeError) unable to encode value: {nil, "key_results"}
web_1  |         (poison) lib/poison/encoder.ex:354: Poison.Encoder.Any.encode/2
web_1  |         (poison) lib/poison/encoder.ex:213: anonymous fn/4 in Poison.Encoder.Map.encode/3
web_1  |         (poison) lib/poison/encoder.ex:214: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3
web_1  |         (poison) lib/poison/encoder.ex:214: Poison.Encoder.Map.encode/3
web_1  |         (poison) lib/poison/encoder.ex:213: anonymous fn/4 in Poison.Encoder.Map.encode/3
web_1  |         (poison) lib/poison/encoder.ex:214: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3
web_1  |         (poison) lib/poison/encoder.ex:214: Poison.Encoder.Map.encode/3
web_1  |         (poison) lib/poison/encoder.ex:213: anonymous fn/4 in Poison.Encoder.Map.encode/3
web_1  |         (poison) lib/poison/encoder.ex:214: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3
web_1  |         (poison) lib/poison/encoder.ex:214: Poison.Encoder.Map.encode/3
web_1  |         (poison) lib/poison/encoder.ex:232: anonymous fn/3 in Poison.Encoder.List.encode/3
web_1  |         (poison) lib/poison/encoder.ex:233: Poison.Encoder.List.encode/3
web_1  |         (poison) lib/poison/encoder.ex:213: anonymous fn/4 in Poison.Encoder.Map.encode/3
web_1  |         (poison) lib/poison/encoder.ex:214: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3
web_1  |         (poison) lib/poison/encoder.ex:214: Poison.Encoder.Map.encode/3
web_1  |         (poison) lib/poison.ex:41: Poison.encode!/2
web_1  |         (ecto) /performancex/deps/postgrex/lib/postgrex/type_module.ex:717: Ecto.Adapters.Postgres.TypeModule.encode_params/3

It works fine with Repo.update so, do you have any idea that could be happening?

@itSQualL itSQualL changed the title Problems updating changesets with relations. Problem updating changesets with relations. May 4, 2017
@itSQualL
Copy link
Author

itSQualL commented May 4, 2017

The problem seems to be here: https://github.com/izelnakri/paper_trail/blob/master/lib/paper_trail.ex#L278-Lundefined

That returns:

item_changes: %{current_status: 19,
  key_results: [#Ecto.Changeset<action: :update,
  changes: %{values: %{"current_value" => 19, "desired_value" => 100}},
  errors: [], data: #Performancex.KeyResult<>, valid?: true>]
}

And fails when you try to do @repo.insert(version)

@izelnakri
Copy link
Owner

item_changes should represent a simple map of the changes automatically generated from your changeset functions. %PaperTrail.Version{item_changes: %{}} is essentially the %Changeset{changes: %{}}. I've never seen a case where you would have a changeset inside a changeset.changes, it should always be a simple map. Are you trying to update relationships in one go? Can you provide the your controller code and the changeset you are updating?

Seems like an interesting edge-case I would really like to know how you've ended up with a changeset inside a changeset. Lastly, thanks for reporting!

@itSQualL
Copy link
Author

itSQualL commented May 4, 2017

Thank you for answering

I have the objective and the params

objective is a struct returned from Repo.get
params are like:

%{
  current_status: 2,
  key_results: [
    id: 2,
    values: %{
      current_value: 15,
      desired_value: 100
    }
  ]
}

Then, I'm just doing:

Objective.changeset_for_update(objective, params)
|> PaperTrail.update(originator: current_user)

Objective module:

def changeset_for_update(struct, params \\ {}) do
  struct
  |> cast(params, [:current_status, :assessment])
  |> cast_assoc(:key_results)
  |> validate_required([:current_status, :key_results])
end

@izelnakri
Copy link
Owner

related discussion: #18. Im closing this issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants