jackc / jackc/pgx

ERROR: 'with' expected (SQLSTATE 00000) 0

Open
#1,954 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

**Describe the bug**
Hi, I am trying to move data from mongodb to postgres using pgx copy from however I am getting an error and the copy count = 0 ERROR: 'with' expected (SQLSTATE 00000)

**To Reproduce**
Steps to reproduce the behavior:

If possible, please provide runnable example such as:

```go
package main

import (
"context"
"fmt"
"log"
"time"

"github.com/jackc/pgx/v5"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)

type Category struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Name struct {
En string `json:"en"`
Ar string `json:"ar"`
} `json:"name"`
Company struct {
Name string `json:"name"`
ID string `json:"_id"`
} `json:"company"`
CompanyRef primitive.ObjectID `json:"companyRef"`
Image string `json:"image"`
Description string `json:"description"`
Status string `json:"status"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

func categories(client *mongo.Client, psql *pgx.Conn) {

db := client.Database("test")
collection := db.Collection("categories")

filter := bson.M{}

cursor, err := collection.Find(context.Background(), filter)

if err != nil {
log.Fatal(err)
}

var results []Category

if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}

copyCount, err := psql.CopyFrom(
context.Background(),
pgx.Identifier{"categories"},
[]string{"categoryNameEn", "categoryNameAr", "companyNameEn", "companyRef", "description", "createdAt", "categoryRef"},
pgx.CopyFromSlice(len(results), func(i int) ([]any, error) {
return []any{results[i].Name.En, results[i].Name.Ar, results[i].Company.Name, results[i].CompanyRef.String(), results[i].Description, results[i].CreatedAt.Format(time.RFC3339), results[i].ID.Hex()}, nil
}),
)

fmt.Println(err, copyCount)

fmt.Println("Bulk insert completed successfully.")

}

```

Please run your example with the race detector enabled. For example, `go run -race main.go` or `go test -race`.

**Expected behavior**
Should be able to copy without any error

**Actual behavior**
![image](https://github.com/jackc/pgx/assets/38590876/b2401ac8-48b2-4be2-aaac-1989d35a66c8)

**Version**
- Go: `$ go version` -> go version go1.22.1 linux/amd64
- PostgreSQL: `$ psql --no-psqlrc --tuples-only -c 'select version()'` -> [e.g. PostgreSQL 14.4 on x86_64-apple-darwin21.5.0, compiled by Apple clang version 13.1.6 (clang-1316.0.21.2.5), 64-bit]
- pgx: `$ grep 'github.com/jackc/pgx/v[0-9]' go.mod` -> v5

**Additional context**
Add any other context about the problem here.

Contributor guide

Open the contributing guide

Research direction

Start with the provided categories function and its pgx.CopyFrom call, then run the example with the Go race detector as requested. Reproduce the PostgreSQL error using the shown MongoDB data and copy arguments; done means identifying the cause of the rejected copy operation and verifying that the corrected example copies rows successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, mongodb, postgres
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.