jmoiron / sqlx

general purpose extensions to golang's database/sql
http://jmoiron.github.io/sqlx/
MIT License
16.3k stars 1.09k forks source link

Batch insert still not working #925

Closed gwd closed 6 months ago

gwd commented 6 months ago

First of all, thank you for sqlx -- very useful package!

I've got the following code, which does a batch insert (ss.Discussions is a slice):

                res, err := sqlx.NamedExec(eq, `
                INSERT INTO event_schedule (discussionid, slotid, locationid)
                    VALUES (:discussionid, :slotid, :locationid)`,
                    ss.Discussions)
                if err != nil {
                    log.Printf("Failed inserting schedule entries %v", ss.Discussions)
                    return errOrRetry("Adding new schedule entries", err)
                }
                if n, err := res.RowsAffected(); err != nil {
                    log.Printf("Error getting rows affected by inserting scheduling entries: %v", err)
                    return errOrRetry("Getting rows affected by inserting schedule entries", err)
                } else if int(n) != len(ss.Discussions) {
                    log.Printf("Error inserting scheduling entries: Expected %d rows affected, got %d!",
                        len(ss.Discussions), int(n))
                    return fmt.Errorf("Inserting scheduling entries: Expected %d rows affected, got %d!",
                        len(ss.Discussions), int(n))
                }

In v1.3.1, this code works, inserting the expected number of rows; but for v1.3.2 and later, it only inserts a single record. I've tried to make it match the example of batch insert with structs as closely as possible, with no luck. Any ideas?

gwd commented 6 months ago

Actually the issue was on a different query, which I've now fixed. Sorry for the noise.