Enable using the attached model during pivot editing
This patch was authored and released by @gwynne.
When using Siblings.attach(_:on:_:)
with an editing closure, this code would historically crash with a fatal error:
planet.$tags.attach([tag1, tag2], on: database) { pivot in
print(pivot.tag.name)
// fatal error: Parent relation not eager loaded, use $ prefix to access: Parent<PlanetTag, Tag>(key: "tag_id")
}
The attach()
methods now provide the "right-hand" side of the relation (e.g. left.$siblings.attach([right1, right2])
) on the pivot model passed to the editing closure.
Unfortunately, due to limitations of Fluent's design and of property wrappers, it is not possible to also provide the "left-hand" side of the relation, but this is usually less of a concern. The expected use case for having the right-hand model available is to be able to get data from the right-hand model for setting properties of the pivot when attaching an array of siblings in one call. Without this change, this was only possible by matching the right-hand model's ID with one from the original input array. (This being said, this change affects the single-model version of attach()
as well, since there is no drawback to doing so.)
This is a solely additive change; it has no effect whatsoever on what data goes into the database, and does not incur any additional speed or memory overhead.