I’m trying to add info into 2 tables inside one transaction, but always getting syntax error, Ineed a first inserted id to insert it into another one, but I also need to make these two inserts both happened or neither of them, I tried a lot of variants but nothing helped, I tried to set variable and used currval(table_id_seq
), but always getting this same syntax error – syntax error (approximate position: “,”).
Maybe I should use something else, I’m all ears what I should fix to get through it.
here is the code in go:
statement, err := s.Db.Prepare("START TRANSACTION; INSERT INTO people(name, surname, patronymic) VALUES(?, ?, ?); INSERT INTO cars(reg_nums, mark, model, year, owner_id) VALUES(?, ?, ?, ?,(select currval('people_id_seq'))); COMMIT;")
if err != nil {
return 0, fmt.Errorf("%s: %w", fn, err)
}
res, err := statement.Exec(car.Owner.Id, car.Owner.Id, car.Owner.Id, car.RegNums, car.Mark, car.Model, car.Year)
Grigory is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.