I am using GORM to run SQL queries. I am trying to Exec an INSERT statement as a TRANSACTION because I need to be able to return the latest inserted ID.
Here is a code snippet:
query := `
START TRANSACTION;
INSERT INTO something (a, b, c)
VALUES (@arg1, @arg2, @arg3);
SELECT LAST_INSERT_ID();
COMMIT;`
result := DB.Exec(
query,
sql.Named("arg1", "aaa"),
sql.Named("arg2", "bbb"),
sql.Named("arg3", "ccc"),
)
I've read through the GORM transactions docs, but the examples are wildly different than my use case.
My goal is to create an INSERT statement and finally return me the latest ID from that create. Using MySQL.
Any ideas?