I have faced a strange issue, after deleting data from one table it is impossible to insert into the table again.
here is an example
I am using postgres DB
_, err = con.Query(`
DELETE FROM loan_aims WHERE _loan = $1;
`, input.ID)
if err != nil {
tx.Rollback()
log.Println(err)
return false, fmt.Errorf("can not delete additional data from loan_aims")
}
stmtAim, err := con.Prepare(`INSERT INTO loan_aims(_loan,_aim) VALUES ($1,$2)`)
if err != nil {
tx.Rollback()
log.Println(err)
return false, fmt.Errorf("can not insert into loan_aims")
}
for i := 0; i < len(aims); i++ {
_, err := stmtAim.Exec(loanID, aims[i])
if err != nil {
tx.Rollback()
return false, fmt.Errorf("can not insert aim 2")
}
}
I have tried to remove this
_, err = con.Query(`
DELETE FROM loan_aims WHERE _loan = $1;
`, input.ID)
if err != nil {
tx.Rollback()
log.Println(err)
return false, fmt.Errorf("can not delete additional data from loan_aims")
}
and it works just fine
Typically, you should use db.Exec()
for DELETE
.
_, err = db.Exec("delete from foo")
if err != nil {
log.Fatal(err)
}