GORM Playground Link

I am able to reproduce the error with postgres dialect https://github.com/go-gorm/playground/pull/635

Description

Contextual information: - Go Version: 1.19 - Gorm Version : v1.25.2-0.20230530020048-26663ab9bf55 - Postgres Driver : postgres v1.5.2

I am trying to use partial indexes to save some data in my table. The idea is that we can have only one unique value for a combination. However we allow someone to delete the value and have multiple rows. Hence created this struct and then created indexes manually

type DynamoDB struct {
    HashKey   string `gorm:"not null"`
    SortKey   string `gorm:"not null"`
    Value     string `gorm:"not null"`
    Tombstone bool   `gorm:"not null;default:false"`
}

func (DynamoDB) TableName() string {
    return "dynamodb"
}

Indexes CREATE UNIQUE INDEX idx_unique ON dynamodb (hash_key, sort_key) WHERE NOT tombstone;

However when i am trying to save data and call this multiple times, it is failing every time at the 6th iteration. Here is the code to save the data

for i := 1; i <= 6; i++ {
        createMetadataVault(DB)
        fmt.Println("ran successfully for iteration: ", i)
}

func createMetadataVault(db *gorm.DB) {
    var metadataModel = DynamoDB{
        HashKey: "hash",
        SortKey: "sort",
        Value:   "value",
    }
    result := db.
        Clauses(clause.OnConflict{
            Columns:     []clause.Column{{Name: "hash_key"}, {Name: "sort_key"}},
            TargetWhere: clause.Where{Exprs: []clause.Expression{clause.Eq{Column: "tombstone", Value: false}}},
            DoUpdates:   clause.AssignmentColumns([]string{"value"}),
        }).
        Create(&metadataModel)

    if result.Error != nil {
        panic("failed to create metadata vault")
    }
}

Comment From: pakdboth

+1

Comment From: a631807682

refer to https://github.com/jackc/pgx/issues/1234