Closed josepfm closed 4 years ago
Can you show the code you're using the save the user?
Of course.
The problem comes in the part of returning a public user.
I tried to create the user without that code and if it works perfectly, so I don't know where the error is.
@jose46moreno ah that makes sense, because things like height
and weight
(which I'm assuming are all children or relationship) haven't been retrieved from the database. So you'll either need a special Public
type that is for something that's just been created (and has no children) or perform the query after the save in the toPublic()
function. Either way you'll need a toPublic()
function that queries everything for when returning users normally
@jose46moreno ah that makes sense, because things like
height
andweight
(which I'm assuming are all children or relationship) haven't been retrieved from the database. So you'll either need a specialPublic
type that is for something that's just been created (and has no children) or perform the query after the save in thetoPublic()
function. Either way you'll need atoPublic()
function that queries everything for when returning users normally
I don't quite understand what you mean exactly.
Even if I don't have any height or weight (child ratio) I shouldn't just return the empty array and that's it?
If you have a proposal for a code, I'd be happy to see it.
Yesterday, while searching, I found this:
As the error says, unless the relation has been eager loaded, you may only access with the $
prefix. For example:
model.myRelation // fatalError, not eager loaded
model.$myRelation.query(on: db).all() // works
This is because the relation must be fetched asynchronously.
As the error says, unless the relation has been eager loaded, you may only access with the
$
prefix. For example:model.myRelation // fatalError, not eager loaded model.$myRelation.query(on: db).all() // works
This is because the relation must be fetched asynchronously.
But I'm not making any calls to get.
Remember that the error occurs when calling toPublic, not any query from the database, so please open the issue again, as this does not solve the problem.
Thank you.
But I'm not making any calls to get.
Not sure what you mean by that. If you attempt to access an @Parent
or @Children
property that is not eager loaded, you will get a crash. That is expected. Those must be accessed using $
prefix as I wrote in my last comment.
So instead of user.height
it must be user.$height.query(...)...
.
so please open the issue again, as this does not solve the problem.
I have closed this because it is neither a bug nor a feature request that needs to be tracked. We can continue to answer your questions even though it has been closed.
@jose46moreno you're implicitly making a call to the database in your toPublic
function by attempting to access the property wrappers, which is why you're getting the error. I suggest that you pass a Request
or Database
object into your toPublic
function and then load the required properties, or don't attempt to query them and just pass an empty array through directly
I have created a parent-child relationship between the following two models, with the following migrations and when I run the Postman screenshot to create a user, I get the error.
Model User:
Model Height:
Migration User:
Migration Height:
Request to create User:
Error:
[ INFO ] query create users input=[[username: "johnsmith", name: "John", password: "$2b$12$xHXMWlIiVJlsVX8JpzG8UuWnpU4lXFseV.WHow1Bh.xvbd.3HbGnq", email: "xxxxx@email.com", image: nil, updated_at: 2020-04-23 16:45:22 +0000, id: D6B703B3-E631-40D9-86AB-0CA0EAF02232, created_at: 2020-04-23 16:45:22 +0000]] Fatal error: Children relation not eager loaded, use $ prefix to access: Children<User, UserWeight>(for: [user_id]): file /Users/josepfm/Library/Developer/Xcode/DerivedData/SSS-Backend-ehnubkdoiqqgavaksaeutibbozzm/SourcePackages/checkouts/fluent-kit/Sources/FluentKit/Properties/Children.swift, line 35