Closed TheJoeSchr closed 4 years ago
For future-reference, here is an excerpt from the discussion of #692:
When you think about what the BelongsToMany
relationship actually is, you will find that it is in fact comprised of a HasMany
and a BelongsTo
relationship.
In terms of your concrete example, you should be able to update a medication like this:
mutation {
updateMedication(input: {
id: 1
name: "Lysergicream"
# That would be a HasMany relationship from Mediation to the pivot model
medicationUser: {
update: [{
dose_mg: 11
# This is a belongsTo relationship from your pivot model to User
user: {
connect: 1
}
}]
}
}) {
id
name
# Will return a list, as this is a HasMany relationship
medicationUser {
dose_mg
# A single BelongsTo
user {
id
name
}
}
}
}
I think we should add a section about how to deal with pivot attributes to the docs. Queries are affected too and should be considered.
@JoeSchr you can now use nested mutations to do this directly and in a convenient way: https://github.com/nuwave/lighthouse/pull/1110
Thanks. Meanwhile I made my custom fork to work around this, but it seems it's finally time to update to this release!
@spawnia How can I write a type that expresses this relationship (beongsToMany + Pivot columns)?
You can use the following channels to ask support questions:
This repository and the issue tracker are used to advance the development of Lighthouse.
Situation
I'm trying to implement @update on a belongsToMany relationship. The pivot table also has columns I want to update, like this:
my example schema:
Problem
I get an SQL error that laravel can't find the columns (of course, they don't exist because they are on the pivot table) on the related table to update.
I debugged it down to the
executeUpdate
function inMutationExecutor.php
, where the pivot property is still inside$remaining
and gets treated as a normal property on the related Model instead of the special case of being pivot columns.Expected
pivot columns should be updated,
Workarounds
there was a workaround suggested:
by @spawnia in this issue thread #549