How to get the inserted id and potentially the other generated values when using Create with a map

huangapple go评论68阅读模式
英文:

How to get the inserted id and potentially the other generated values when using Create with a map

问题

我正在尝试使用GORM创建一些灵活的CRUD函数,并通过map[string]any传递值。

以下代码可以正确构建查询:

db.Table(model.TableName()).Clauses(clause.Returning{
	Columns: []clause.Column{{Name: "id"}},
}).Create(theMap)

但问题是...我如何将id的值传回Go语言中呢?谢谢!

英文:

I am trying to make some flexible CRUD functions using GORM and passing the values via a map[string]any.

The query is built properly with the following code:

db.Table(model.TableName()).Clauses(clause.Returning{
Columns: []clause.Column{{Name: "id"}},
}).Create(theMap)

But the question is... how do I get the value of id back into Go? Thanks!

答案1

得分: 2

我找到解决方案了...

db.Table(model.TableName()).Clauses(clause.Returning{}).Create(theMap)

上述查询将把所有列加载回到映射中。

英文:

NVM I found the solution...

db.Table(model.TableName()).Clauses(clause.Returning{}).Create(theMap)

The above query will load all columns back to the map.

huangapple
  • 本文由 发表于 2023年2月26日 04:59:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75568394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定