M
M
Magus_Education2021-04-19 20:12:10
go
Magus_Education, 2021-04-19 20:12:10

Why is db.Find eating one of the table values?

Good afternoon. I read in the official documentation that db.find is essentially select * from, but using select * from through MySQL I get . 607db90eac95e638386069.png
Whereas using db.Find in the code gives me 607db9695cb57974415719.png
a Question. Why does db.Find eat userId and instead of the required 7, it always returns 0?

Structure in code:
type Post struct {
ID int `gorm:"primaryKey"`
UserId int `json:"userId"`
Title string `json:"title"`
Body string `json:"body"`
}
DB table:
Columns :
id int PK
userId int
title varchar(100)
body varchar(250)

PS I apologize wildly for the wildness of the second screenshot, here is an example in letters: "ID": 61, "userId": 0, "title": "voluptatem doloribus consectetur est ut ducimus", "body": "ab nemo optio odio\ndelectus tenetur corporis similique nobis repellendus rerum omnis facilis\nvero blanditiis debitis in nesciunt doloribus dicta dolores\nmagnam minus velit"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-04-19
@Magus_Education

99%, it's about the field names
If you extract data from the database into the Post structure, add db:"userId" there to get something like this
UserId int `json:"userId" db:"userId"`
or for gorm
UserId int `json:"userId" gorm:"column:userId"`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question