fix: do nothing by duplicate key

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-07-03 09:52:39 +02:00
parent 0a5f281a1e
commit c20b9143db

View File

@ -3,6 +3,7 @@ package postgres
import (
"context"
"errors"
"gorm.io/gorm/clause"
otterError "git.dragse.it/anthrove/otter-space-sdk/v2/pkg/error"
@ -220,7 +221,10 @@ func CreateTagAliasInBatch(ctx context.Context, db *gorm.DB, tagAliases []models
return &otterError.EntityValidationFailed{Reason: "batch size cannot be zero"}
}
result := db.WithContext(ctx).CreateInBatches(tagAliases, batchSize)
result := db.WithContext(ctx).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "name"}},
DoNothing: true,
}).CreateInBatches(tagAliases, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}