Why after deleting some data from the database it does not insert into same table


(AkezhanOb1) #1

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

_, 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")
	}
}

it does not work, inside my database loan_aims table stays empty, but if I remove

_, 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")
}

it works fine, btw I am using postgres


(Lutz Horn) #2

What do you mean by “it does not work”? Is any of the errors returned?