gorm uniqueindex有效,但没有存储数据
因此,我有一个数据库: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论