After go gorm select, the result set or all struct are returned, except that the other fields are empty.

after go gorm select, the result set or all struct are returned, except that the other fields are empty

the environmental background of the problems and what methods you have tried

related codes

db.Debug (). Where (s). Select ([] string {"id", "username", "phone"}). Find (& user)

type User struct {

gorm.Model
Username string `json:"username"`
Phone string `json:"phone"`
Type int8 `json:"type"`
Order []Order `gorm:"ForeignKey:UId"`   // hasMany 
CreditCard *CreditCard `gorm:"foreignkey:CardID"`
CardID uint

}

actual results

[
{
"id": 3,
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"deleted_at": null,
"username": "hello",
"phone": "18672858778",
"type": 0,
"Order": null,
"CreditCard": null,
"CardID": 0
},
{
"id": 6,
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"deleted_at": null,
"username": "hello",
"phone": "18672858778",
"type": 0,
"Order": null,
"CreditCard": null,
"CardID": 0
},
{
"id": 9,
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"deleted_at": null,
"username": "hello",
"phone": "18672858779",
"type": 0,
"Order": null,
"CreditCard": null,
"CardID": 0
},
{
"id": 12,
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"deleted_at": null,
"username": "hello",
"phone": "18672858779",
"type": 0,
"Order": null,
"CreditCard": null,
"CardID": 0
}
]

expected results

[{
"id":6,
"username":"hello",
"phone":"18672858779"
},
{
"id":9,
"username":"hello",
"phone":"18672858779"
}
]
Mar.31,2021

because you use yes:

   

The result generated by

is inconsistent with Alarm struct, resulting in the returned information is the value of the original Alarm struct (in some unavailable errors, of course).
ask the boss to explain how to return correctly, such as the correct data displayed in the database.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea0c29-4efd1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea0c29-4efd1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?