go version)?go version go1.10.3 linux/amd64
Last version of gorm and postgresql
type AttachmentsNew struct {
gorm.Model
id intgorm:"AUTO_INCREMENT"
ownerId int
hashkey stringgorm:"size:64"
time int
fileInfo string
filepath string
doctype stringgorm:"size:15"`
}
Database.Create(&AttachmentsNew{ownerId: ownerId, hashkey: "test", time:0, fileInfo:file.oldname, filepath:file.path, doctype:file.documentType})`
How i can get id of inserted value?
row := new(Article)
d := Gorm.Create(article).Scan(&row)
if d.Error != nil {
return 0, d.Error
}
return row.Id, nil
My ID is autoincrement.
Your code return ID - 0, buy ID is 6;
Gorm automatically inserts the ID into the struct.
a := &AttachmentsNew{ownerId: ownerId, hashkey: "test", time:0, fileInfo:file.oldname, filepath:file.path, doctype:file.documentType}
Database.Create(a)
// a.id should be populated
Populated means extended?
@DuckerMan yes it means "filled".
So, why i should create variable that point to my structure?
so you can get use the ID after the creation. Otherwise I don't know any other method to achieve this.
Most helpful comment
row := new(Article)
d := Gorm.Create(article).Scan(&row)
if d.Error != nil {
return 0, d.Error
}
return row.Id, nil