volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.56k stars 533 forks source link

Bug/breaking change on upsert with postgres in v4.16.0 #1341

Closed eirikbell closed 5 months ago

eirikbell commented 5 months ago

What version of SQLBoiler are you using (sqlboiler --version)?

SQLBoiler v4.16.0

What is your database and version (eg. Postgresql 10)

postgresql 14

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

If this happened at runtime what code produced the issue? (if not applicable leave blank)

func TestSqlBoiler(t *testing.T) {
    db, err := initializeDb(t)
    if err != nil {
        t.Fatal(err)
    }
    forinsert := &dbmodels.Mytable{ID: uuid.New().String(), Name: "test"}
    if err := forinsert.Insert(context.Background(), db, boil.Infer()); err != nil {
        t.Fatal(err)
    }

    forupsert := &dbmodels.Mytable{ID: uuid.New().String(), Name: "test"}
    if err := forupsert.Upsert(context.Background(), db, true, []string{"id"}, boil.Infer(), boil.Infer()); err != nil {
        t.Fatal(err)
    }
    forupsert.Name = "test2"
    if err := forupsert.Upsert(context.Background(), db, true, []string{"id"}, boil.Infer(), boil.Infer()); err != nil {
        t.Fatal(err)
    }
}

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

» go test ./... -count=1 --cover -v
?       boil/dbmodels   [no test files]
=== RUN   TestSqlBoiler
Debug: INSERT INTO "mytable" ("id","name") VALUES ($1,$2)

Debug: [132cff82-ee1a-47d2-967b-681f6951015a test]

Debug: INSERT INTO "mytable" (""id"",""name"") VALUES ($1,$2) ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name" RETURNING "id"

Debug: [e65f2549-9a95-4030-8af5-5b5ad7b6509d test]

    main_test.go:49: dbmodels: unable to upsert mytable: pq: zero-length delimited identifier at or near """"
--- FAIL: TestSqlBoiler (0.02s)
FAIL
FAIL    boil    0.208s
FAIL

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

CREATE TABLE IF NOT EXISTS mytable
(
    "id" VARCHAR(128) PRIMARY KEY NOT NULL CHECK ("id" <> ''),
    "name" VARCHAR(100) NOT NULL CHECK ("name" <> '')
);

Further information. What did you do, what did you expect?

When generating with SQL boiler v4.15.0 and adding go mod dependencies for github.com/volatiletech/sqlboiler/v4 v4.15.0 and github.com/volatiletech/strmangle v0.0.5, the test passes and outputs:

Debug: INSERT INTO "mytable" ("id","name") VALUES ($1,$2)

Debug: [51194c00-09a8-4646-8f7a-9a37ec690d24 test]

Debug: INSERT INTO "mytable" ("id", "name") VALUES ($1,$2) ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name"

Debug: [c9f4e751-9b3a-4f71-8aea-6684208d6529 test]

Debug: INSERT INTO "mytable" ("id", "name") VALUES ($1,$2) ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name"

Debug: [c9f4e751-9b3a-4f71-8aea-6684208d6529 test2]

Changing to SQL boiler v4.16.0 and adding go mod dependencies for github.com/volatiletech/sqlboiler/v4 v4.16.0 and github.com/volatiletech/strmangle v0.0.6, the test fails and outputs:

Debug: INSERT INTO "mytable" ("id","name") VALUES ($1,$2)

Debug: [132cff82-ee1a-47d2-967b-681f6951015a test]

Debug: INSERT INTO "mytable" (""id"",""name"") VALUES ($1,$2) ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name" RETURNING "id"

Debug: [e65f2549-9a95-4030-8af5-5b5ad7b6509d test]

    main_test.go:49: dbmodels: unable to upsert mytable: pq: zero-length delimited identifier at or near """"

The resulting sql statement for upsert changes for the upsert call causing error returned from generated code:

4.15.0: INSERT INTO "mytable" ("id", "name") VALUES ($1,$2) ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name"
4.16.0: INSERT INTO "mytable" (""id"",""name"") VALUES ($1,$2) ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name" RETURNING "id"

not sure if its the double quotes or RETURNING "id" causing the failure but based on error message I would guess its the double quotes

stephenafamo commented 5 months ago

I suspect it has something to do with #1322

Unfortunately, I do not have the bandwidth to debug this now, but feel free to send in a PR and I'll review and release.