Error when trying to open a link to an acronym

In Chapter 22 “Users and Categories”, just before the last change in the code for acronymHandler to add the categories, running TILApp produces the desired result when tapping an acronym link - the acronym info page is displayed. When you change the code from the following:

func acronymHandler(_ req: Request) throws → Future {
return try req.parameter(Acronym.self).flatMap(to: View.self) { acronym in
return acronym.creator.get(on: req).flatMap(to: View.self) { creator in
let context = AcronymContext(title: acronym.long, acronym: acronym, creator: creator)
return try req.leaf().render(“acronym”, context)
}
}
}

to the following:

func acronymHandler(_ req: Request) throws → Future {
return try req.parameter(Acronym.self).flatMap(to: View.self) { acronym in
return try flatMap(to: View.self, acronym.creator.get(on: req), acronym.categories.query(on: req).all()) { creator, categories in
let context = AcronymContext(title: acronym.long, acronym: acronym, creator: creator, categories: categories.isEmpty ? nil : categories)
return try req.leaf().render(“acronym”, context)
}
}
}

after adding categories to the AcronymsContext struct, the link from an acronym is broken. (In the browser, you get “Oops: This parent relationship could not be resolved” and the message in the Xcode console is “[ ERROR ] FluentError.parentRequired: This parent relationship could not be resolved (Parent.swift:38)”.

Remove the code for the categories from acronymHandler and the app works again without errors.

The code downloaded from Chapter 22 in the video course shows the same issue.

@0xtim Do you have any feedback regarding this? Thank you - much appreciated! :]

@rcutshaw can you try running swift package update && vapor xcode -y and then trying again? It might be an old bug that’s been fixed.

(This will probably throw up a load of warnings about extra trys that will be fixed in the next release)

PS is this chapter 17? I’ve only just written Chapter 22!