Your Question
Hello, everyone!
In our project, we are using the COPY FROM STDIN statement to perform bulk imports in a PostgreSQL database. This code works fine with GORM v1.9.16, but when I upgraded GORM to v1.25.6, I encountered the following error: sql: expected 0 arguments, got 8
I searched the GORM documentation for that, but couldn't find anything. I also checked the release notes but didn't find any information.
type BulkImporter struct {
tx *sql.Tx
stmt *sql.Stmt
}
// Info: https://pkg.go.dev/github.com/lib/pq?utm_source=godoc#hdr-Bulk_imports
func NewBulkImporter(db *sql.DB, table string, columns ...string) (*BulkImporter, error) {
tx, err := db.Begin()
if err != nil {
return nil, err
}
stmt, err := tx.Prepare(pq.CopyIn(table, columns...))
if err != nil {
rollback(tx)
return nil, err
}
return &BulkImporter{
tx: tx,
stmt: stmt,
}, nil
}
func (b *BulkImporter) Add(args ...interface{}) error {
_, err := b.stmt.Exec(args...)
if err != nil {
abort(b.stmt, b.tx)
}
return err
}
func (b *BulkImporter) Commit() error {
_, err := b.stmt.Exec()
if err != nil {
abort(b.stmt, b.tx)
return err
}
err = b.stmt.Close()
if err != nil {
rollback(b.tx)
return err
}
err = b.tx.Commit()
if err != nil {
rollback(b.tx)
}
return err
}
The error occurs when executing the Add function.
func (s *AvailabilityStore) startSaver() {
for arr := range s.createBatchCh {
sqlDB, err := s.db.DB()
if err != nil {
logwrapper.Logger.Fatal("can't get db instance", err)
continue
}
importer, err := postgres.NewBulkImporter(sqlDB, model.KpiAvailabilityTableName, model.KpiAvailabilityInsertColumns...)
if err != nil {
logwrapper.Logger.Errorf("create bulk importer error: %v", err)
continue
}
now := time.Now()
for i := range arr {
// "project_id", "task_id", "data", "grouped", "duration", "unix_time", "created_at", "updated_at"
data, err := json.Marshal(arr[i].stat.Data)
if err != nil {
continue
}
err = importer.Add(
arr[i].stat.ProjectID,
arr[i].stat.TaskID,
string(data),
arr[i].stat.Grouped,
arr[i].stat.Duration,
arr[i].stat.UnixTime,
now,
now,
)
if err != nil {
logwrapper.Logger.Errorf("error bulk import record value=%+v: %v", arr[i], err)
break
}
}
err = importer.Commit()
if err != nil {
logwrapper.Logger.Errorf("error commit bulk import: %v", err)
}
pipe := s.rdb.Pipeline()
for _, v := range arr {
pipe.HDel(v.key, strconv.Itoa(int(v.stat.UnixTime)))
}
_, err = pipe.Exec()
if err != nil {
logwrapper.Logger.Errorf("error executing pipeline after bulk import: %v", err)
}
arr = arr[:0]
batchSavePool.Put(arr)
}
}
The document you expected this should be explained
Expected answer
Why is this happening, and how can I fix it, or what is an alternative approach for this?
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days