如何检索最后添加的项目的实例 [英] How can I retrieve instance of last added item
本文介绍了如何检索最后添加的项目的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将github.com/jinzhu/gorm与mysql后端一起使用.我想在上一个Create调用中检索该行的ID(或完整实体).
I'm using github.com/jinzhu/gorm with a mysql backend. I want to retrieve the Id (or the full entity) of the row in the previous Create call.
例如,last-insert-id :( http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id )
As in, last-insert-id: (http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id)
我该怎么做?
推荐答案
type User struct {
Id int
Name string
}
user := User{Name: "jinzhu"}
db.Save(&user)
// user.Id is set to last insert id
这篇关于如何检索最后添加的项目的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文