go version go1.17.6 windows/amd64 gorm.io/driver/mysql v1.3.2 gorm.io/gorm v1.23.2

表结构:

type Users struct {
    Id           int64
    Username     string       `gorm:"size:64;uniqueIndex:username_project"`
    Password     string       `gorm:"size:512"`
    ProjectName  string       `gorm:"uniqueIndex:username_project"`
}


type Projects struct {
    Id      int64     `gorm:"primaryKey"`
    Name    string    `gorm:"size:64;unique"`
    Users   []Users   `gorm:"foreignKey:ProjectName;references:Name;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
}

创建Users并添加关联时:

user:= Users{
    Username: "tony",
    Password: "123",
}
if err := DB.First(&models.Projects{}, id).Association("Users").Append(&user); err != nil {
    return false, err
}
return true, nil

第一次添加关联时可以正确创建Users并关联Projects。

再次创建时应该引发Duplicate entry 'xxx' for key 'projects.name' 异常 但是即得不到error也不能正确创建Users和添加关联

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 30 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: jinzhu

it will ignore those duplicated when creating associations