It looks like something wrong with the id
field mapping;
the sql it generated is SELECT * FROM
addressesWHERE (id = '1')
;
but in my struct id
field allways be 0;
You'll need to provide more information on what you were trying to do and how your struct is designed and what exactly you did.
ok,my code is:
type Address struct {
BaseModel
UserId string `json:"userId" gorm:"column:user_id;not null"`
Name string `json:"name" gorm:"not nul"`
Mobile string `json:"mobile" gorm:"not null"`
AddrStr string `json:"addrStr"`
IsDefault int `json:"isDefault" gorm:"column:is_default"`
}
baseModel is:
type BaseModel struct {
ID uint `gorm:"primary_key;column:id" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt *time.Time `sql:"index" json:"deletedAt,omitempty"`
}
then I use gin web framework write like this:
addrRouter.GET("/:id", func(c *gin.Context) {
var addr Address
db.Where("id = ?", c.Param("id")).Find(&addr)
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "info": &addr})
})
then the response body is
{
"info": {
"id": 0,
"createdAt": "0001-01-01T00:00:00Z",
"updatedAt": "0001-01-01T00:00:00Z",
"userId": "123123121",
"name": "FFFF",
"mobile": "18989898989",
"addrStr": "xxxxxxxx",
"isDefault": 0
},
"status": 200
}
should I must extends gorm.Model
Log the actual struct and show here. Not the json.
log.printf("%+#v", addr)
You don't need to extend gorm.Model. I never do because I never soft delete and always define my primary key as Id
in my struct.
I'm so sorry,I have found the reason, it's nothing to do with gorm, thank you for your time
@FoghostCn I'm experiencing the same issue, how did you fix it?
cause my base model name not begin with a capital letter, baseModel
-> BaseModel
then it work
@FoghostCn thanks for your reply.
I just realized that If database column names contains only capitalized letters, gorm cannot map the result set (for find, for instance) to golang's struct.
Another approach is to tell gorm that the database name is all capitalized, through golang annotations (gorm:"column:COLUMN_X" ).
Would that be the case to add more information about it on gorm guidelines/README.md, because it does not throws any errors/warnings and it became difficult to debug the problem.
I have the same problem
log.printf("%+#v", user)
&models.User{Model:gorm.Model{ID:0x1, CreatedAt:time.Time{wall:0x0, ext:63643335271, loc:(time.Location)(nil)}, UpdatedAt:time.Time{wall:0x0, ext:63643335271, loc:(time.Location)(nil)}, DeletedAt:(time.Time)(nil)}, ID:0, Fullname:"", Password:"1234", CreatedAt:time.Time{wall:0x0, ext:0, loc:(time.Location)(nil)}, UpdatedAt:time.Time{wall:0x0, ext:0, loc:(time.Location)(nil)}, DeletedAt:(time.Time)(nil)}
log.printf("%+v", user)
&{Model:{ID:1 CreatedAt:2017-10-11 16:14:31 +0000 UTC UpdatedAt:2017-10-11 16:14:31 +0000 UTC DeletedAt:
I had a similar problem, except that all the fields are set to 0 / null. In my case it was because I didn't have a proper error handling, and when I actually wanted to query the database, the sql connection was already closed sql: database is closed
.
I had to move the defer ...b.Close()
call to the main function (let's skip the details).
As a rule of thumb, I'd always recommend to finish the query statement with .Error
and
# PostgreSQL jsonb query
err = models.Db.Where("details ->> 'type' = 'foo'").First(&result).Error
if err != nil {
panic(err) // or log.Fatal
}
You should never close db @januszm
I have the same issue with regular queries. As you can see I query three different records, all fields are fine except for the id field. It always returns 0.
profile1 := model.Profile{}
profile2 := model.Profile{}
profile3 := model.Profile{}
db.Where("Id = ?", 11).First(&profile1)
db.Where("Id = ?", 12).First(&profile2)
db.Where("Id = ?", 13).First(&profile3)
fmt.Printf("%+#v", profile1)
fmt.Printf("%+#v", profile2)
fmt.Printf("%+#v", profile3)
model.Profile{Id:0, IsActive:1, StartHour:8, StartMinute:50, Name:"test1", Monday:1, Tuesday:1, Wednesday:1, Thursday:1, Friday:1, Saturday:1, Sunday:0, IsPassive:1, StartingDate:""}
model.Profile{Id:0, IsActive:1, StartHour:16, StartMinute:20, Name:"test2", Monday:1, Tuesday:1, Wednesday:1, Thursday:1, Friday:1, Saturday:1, Sunday:0, IsPassive:1, StartingDate:""}
model.Profile{Id:0, IsActive:1, StartHour:1, StartMinute:20, Name:"test3", Monday:1, Tuesday:1, Wednesday:1, Thursday:1, Friday:1, Saturday:1, Sunday:0, IsPassive:1, StartingDate:""}
cause my base model name not begin with a capital letter,
baseModel
->BaseModel
then it work
old code:
type commonModelFields struct {
ID uint `gorm:"primary_key;column:id" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
}
c => C
type CommonModelFields struct {
ID uint `gorm:"primary_key;column:id" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
}
It works for me. Tks
Most helpful comment
I had a similar problem, except that all the fields are set to 0 / null. In my case it was because I didn't have a proper error handling, and when I actually wanted to query the database, the sql connection was already closed
sql: database is closed
.I had to move the
defer ...b.Close()
call to the main function (let's skip the details).As a rule of thumb, I'd always recommend to finish the query statement with
.Error
and