gorm uniqueindex有效,但没有存储数据

发布于 2025-01-21 03:24:50 字数 2060 浏览 0 评论 0原文

因此,我有一个数据库:3个表:债权人,债务人和经营,该行动是指债权人和债务人,如果该债务人已经在表格中,我不希望该债务人创建一个新的行,但对于债务人来说是相同的,但是该行动必须在数据库债务人或/和债权人中已经存在的ID存储。

因此,为了尝试做到这一点,我将唯一的IndiNdex:IDX_NAME放置在仅存储数据(name/bic或name/iban)是唯一的,因此它的好处,但是问题是GORM给出了此错误并且没有存储操作。

Error 1452: Cannot add or update a child row: a foreign key constraint fails (`mybb`.`operations`, CONSTRAINT `fk_operations_creditor` FOREIGN KEY (`creditor_id`) REFERENCES `creditors` (`id`))

有什么办法做到这一点吗?或者我必须单独检查债务人和债权人是否存在,如果不是,则保存它,如果是的话,请获得ID,然后保存操作?

结构

type Creditor struct {
    gorm.Model
    Name string `json:"name" gorm:"uniqueIndex:idx_name"`
    BIC  string `json:"bic" gorm:"uniqueIndex:idx_name"`
}
type Operation struct {
    gorm.Model
    Debtor     *Debtor
    Creditor   *Creditor
    Type       int    `json:"type"`
    Gateway    string `json:"gateway"`
    DebtorID   int    `json:"debtor_id"`
    CreditorID int    `json:"creditor_id"`
}
type Debtor struct {
    gorm.Model
    Name string `json:"name" gorm:"uniqueIndex:idx_name"`
    IBAN  string `json:"iban" gorm:"uniqueIndex:idx_name"`
}

代码

    var err error
    dsn := "root:changeme@tcp(localhost:3306)/mybb?charset=utf8mb4&parseTime=True&loc=Local"
    conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
    if err != nil {
        println(err.Error())
    }
    conn.AutoMigrate(Operation{})
    op := make([]Operation, 0)
    op = append(op, Operation{
        Debtor: &Debtor{
            IBAN: "test",
            Name: "hey"},
        Creditor: &Creditor{
            Name: "otr",
            BIC:  "hello"},
        Type:    2,
        Gateway: "sepa",
    })
    op = append(op, Operation{
        Debtor: &Debtor{
            IBAN: "test",
            Name: "hey",
        },
        Creditor: &Creditor{
            Name: "otr",
            BIC:  "hello",
        },
        Type:    2,
        Gateway: "sepa",
    })
    res := conn.Create(op)
    if res.Error != nil {
        println(res.Error.Error())
    }

so I have a database which 3 tables: creditor, debtor and operation, the operation are refer to a creditor and a debtor if this debtor is already in the table I dont want that create a new row, the same for debtor, but the operation have to be storage with the id of the already in database debtor or/and creditor.

So for try to make this I put the uniqueIndex:idx_name which only storage the data if both (name/bic or name/iban) are unique, so that its good, but the problem is gorm give this error and doesnt storage the operation.

Error 1452: Cannot add or update a child row: a foreign key constraint fails (`mybb`.`operations`, CONSTRAINT `fk_operations_creditor` FOREIGN KEY (`creditor_id`) REFERENCES `creditors` (`id`))

Is there any way to do this? or do I have to separately check if the debtor and creditor exist, save it if not or get the id if yes and later save the operation?.

Structures

type Creditor struct {
    gorm.Model
    Name string `json:"name" gorm:"uniqueIndex:idx_name"`
    BIC  string `json:"bic" gorm:"uniqueIndex:idx_name"`
}
type Operation struct {
    gorm.Model
    Debtor     *Debtor
    Creditor   *Creditor
    Type       int    `json:"type"`
    Gateway    string `json:"gateway"`
    DebtorID   int    `json:"debtor_id"`
    CreditorID int    `json:"creditor_id"`
}
type Debtor struct {
    gorm.Model
    Name string `json:"name" gorm:"uniqueIndex:idx_name"`
    IBAN  string `json:"iban" gorm:"uniqueIndex:idx_name"`
}

Code

    var err error
    dsn := "root:changeme@tcp(localhost:3306)/mybb?charset=utf8mb4&parseTime=True&loc=Local"
    conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
    if err != nil {
        println(err.Error())
    }
    conn.AutoMigrate(Operation{})
    op := make([]Operation, 0)
    op = append(op, Operation{
        Debtor: &Debtor{
            IBAN: "test",
            Name: "hey"},
        Creditor: &Creditor{
            Name: "otr",
            BIC:  "hello"},
        Type:    2,
        Gateway: "sepa",
    })
    op = append(op, Operation{
        Debtor: &Debtor{
            IBAN: "test",
            Name: "hey",
        },
        Creditor: &Creditor{
            Name: "otr",
            BIC:  "hello",
        },
        Type:    2,
        Gateway: "sepa",
    })
    res := conn.Create(op)
    if res.Error != nil {
        println(res.Error.Error())
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文