How to handle linked models?


I have two models, using Go 1.19:

type User struct {
    Name string
    ID   int

type Order struct {
    ID   int
    Name string
    User *User
    // or
    UserID int

Of course, the database orders table has a foreign key to the users table via user_id.

Probably in different situations I have to use one of these models. When exactly?
Mb only user_id in DTO models, the user in responses from the server?

I will be glad for any information 🙂


It depends on your purpose. As usual, you have to use id when a table to include has meta info about your entity (often it’s tables with a lot of rows and so heavy), therefore it will be better to use id, otherwise if it’s table which describe some fields in initial table, you can use full entity.

Answered By – DoKep

Answer Checked By – Gilberto Lyons (GoLangFix Admin)

Leave a Reply

Your email address will not be published.