-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
How to Use 1-Many (yes, I read the docs carefully) #6254
Comments
I don't understand your question. |
@a631807682 What's not working is that when I query User, to return an instance, the value for Roles is not populated. I'm following the documentation for how to setup 1->Many as specified in the documentation, and it doesn't work. I'd like to know how to make it work. In particular, I don't understand how I can query the roles and get data as expected but when I'm embedding the Roles onto the user, it doesn't even though it's precisely following the example (credit cards) in the documentation. |
One additional note. I revisited the has_many page, and notice that it calls a "Preload()" method to indicate the linked slice should be loaded. I added a Preload() to my statement and it didn't make any difference. My current query statement is:
and for the single record:
|
@a631807682 I have a different test case that doesn't involve the view. I have my user model as documented above. Then, I have another model, ItemChecksum
when I query, I get:
So it's working better, in that the LastEditedBy JSON object is present, but it doesn't have any data. I setup a logger to run at info level, and I can see the call made to read the record, but no references to the user table are happening.
I've tried every combination that I can think of for foreignKey and references and I can't seem to figure it out. I also tried calling Preload with clause.Associations per the docs, and that didn't change anything either. Perhaps if you can help me with this, the other would work. Just to confirm something: The auto migrate calls really only update the schema to match the model correct? There's not a dependency on calling AutoMigrate to populate some sort of information/linkage table right? |
Yes, we determine the table relationship based on parsing struct and tag, AutoMigrate is not necessary.
If your table relationships match what we parsed, Preload should work. It seems to me that what you describe is part of the thing and not the whole thing, so I hope you can provide easy reproduction code, including |
@a631807682 You're correct. I wasn't showing the whole picture. I made a simplified test case and was able to sort out the problem. What I was trying to do was use interfaces to create a generic REST API endpoint. Here's an example:
The code to do the queries was:
This code actually worked in that I would get a single player, or a collection of players, but the Team value wasn't loaded. In my production code, I had two problems. The first was I had a function:
what I found is that the type of v and sl was *interface{}. Evidently, when named results are used, the variables are initialized to zero of the type. In this case, for the interface, the dynamic type was *interface{} and the value was initially nil. When this code is used:
then v is *models.Player, and sl is *[]models.Player. Once I had that, and understood the types I was returning, then I saw that I needed to change the calls to be just the variable, not the address of the variable:
Once those two changes were made, the Pre loading worked as expected. For my original effort with []UserRole, I found the correct definition to be:
|
Your Question
I'm at a loss how to make this work. I've extensively experimented, and reviewed the documentation, and I'm just not seeing how to make this go. I'm trying to embed a user's roles in the User model, and it's returning null. I have two models:
and:
vUserRoles is:
I can retrieve the roles directly:
returns:
I tried defining:
as well as:
and it doesn't work. They don't generate an error, but roles is null.
generates an error.
The document you expected this should be explained
https://gorm.io/docs/has_many.html
Expected answer
I'd like to know how to have the value for roles populate. I'd really appreciate some help with this. Thanks.
The text was updated successfully, but these errors were encountered: