jackc / pgx

PostgreSQL driver and toolkit for Go
MIT License
10.63k stars 837 forks source link

ERROR: 'with' expected (SQLSTATE 00000) 0 #1954

Open delusion8399 opened 7 months ago

delusion8399 commented 7 months ago

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:

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

Version

Additional context Add any other context about the problem here.

jackc commented 7 months ago

Nothing jumps out at me. Try to isolate this example to a reproduction case that doesn't require Mongo. Perhaps manually construct the slice to pass to CopyFrom that demonstrates the problem.