-
-
Notifications
You must be signed in to change notification settings - Fork 267
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
Load relation without loading foreign key #16
Comments
Make sure you have the 'model' value set in $attributes for both UserType and DeviceType, as shown here: https://github.com/rebing/graphql-laravel#creating-a-query Also, make sure Device Model has defined its primary key (check the Eloquent Model class): |
The $attributes value is set in both types. Did you mean to write The attributes are also defined in the relation - why do I need to define the $primaryKey as well? This would also not work in case the model has multiple primaryKey depending on different relations. The relations look like this:
|
Looking at your relation, it seems like User's primary key is account_id, not id, as you said earlier. The package uses the $primaryKey attribute to automatically select the correct keys for relations and they need to be defined in the Model, if they are not the default value (id). |
account_id is the attribute in Devices. User has only an id-attribute. This still does not work:
|
Does Can you show me those 2 database tables and their foreign keys? |
I appreciate your help! So the structure is as follows. User Table:
User Model:
Device Table:
Device Model:
|
I'm sorry, but I can't find any mistake :( Did you try it without pagination? Are you returning GraphQL::paginate('user') with the query? You can post the full Query and maybe I'll find what's wrong |
I turned off the pagination by defining the type in the UserQuery as This will work as expected and return the information I want for the device without needing to query the id of the user as well.
Do you have an idea why this wont work any more with pagination? |
You are correct, it's not working correctly with pagination! The mistake is probably somewhere inside the SelectFields class, but I currently don't have time to find it. You can try to solve it yourself and submit a PR or just use the current workaround and attach the |
Thanks for the hint. I'll look into in when I find the time. |
Same issue, relations belongsTo does not return data |
I have the following relation:
User has many devices, and a device belongs to a user.
User has an id.
And a device has an user_id.
The following query will return the device information as desired:
This query wont find any information devices (when leaving out the id):
Is this part of the design?
Or is there a way where I don't need to query the foreign key (user.id) explicitly?
The text was updated successfully, but these errors were encountered: