func (c *OlapMetadataDetailDao) BatchSave(m []MetadataDetail) error {
db := c.dbClient.GetClient().(*gorm.DB)
result := db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "a_id"}, {Name: "code"}},
DoUpdates: clause.AssignmentColumns([]string{"name", "type", "update_time"}),
}).Create(&m)
return result.Error
}
- 新创建时候没问题可以正确返回id
- 当索引冲突后修改其他字段返回id会返回当前表最大Id加1
- 单记录操作没问题只会在批量操作时候发生
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: lifelmy
这个问题解决了吗
Comment From: Tifinity
我也遇到相同问题,这个问题怎么解决的?
Comment From: guanhonly
我单条记录都出现了问题,请问用的是哪个版本的gorm
Comment From: CaiJinKen
@jinzhu 这个问题依然存在的。同 https://github.com/go-gorm/gorm/issues/6047
Comment From: CaiJinKen
@jinzhu 这个问题依然存在的。同 #6047
我找到一个解决方法:Create 换成 CreateInBatches ,同时 batchSize 设置为1,经过我的测试,这样结果是正确的,但是效率比batchSize要低一些
Comment From: jinzhu
If your database does not use a standard auto-increment of +1, you might need to configure the AutoIncrementIncrement attribute in your GORM model.
If you need to define the AUTOINCREMENTINCREMENT setting directly on a model field in GORM, you can do so using GORM's field-level tags. This approach allows you to specify the auto-increment behavior for a specific field within your model. Here's an example in English:
type MyModel struct {
ID uint `gorm:"autoIncrementIncrement:5"` // Set auto-increment increment to 5
Name string
// other fields
}
In this example, the ID field of MyModel is set to auto-increment with an increment value of 5. This means that each new record will have its ID increased by 5 compared to the previous record. This is useful in cases where your database does not follow the default auto-increment behavior of incrementing by 1.