db.Preload problem

Hello, here is the code:

This result := db.Preload(“User”).Find(&articles) gives me the error “&{0xc0000fc2d0 User: unsupported relations for schema Article 2 0xc0002f8540 0}”.

Can you help me?

This is more a Gorm question than a Go question. That said, article doesn’t have a user associated with it so Gorm doesn’t know how to preload users. You probably want to define a relationship between those structs/tables:

In this case maybe article has a user object as the author. Also why do you have multiple versions of user/article structs? Probably best to unify those.

Hello Dean,
The code I gave contains 3 files. The service and the User/Article models.
I have a foreign key in Articles and it works fine. The problem comes when using Preload.

You Article struct doesn’t have a User struct as a property. Refer to that link I posted for information on defining an association as well as this docs page for preloading:

You’re trying to db.Preload("User") but you don’t have a user object to preload on your Article struct, hence the error. You need to define the association for it to work.

Thanks. I decided to use SQL for my little course project.