lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
8.98k stars 909 forks source link

CREATE/DROP DATABASE cannot run inside a transaction block #1012

Closed dfang closed 2 years ago

dfang commented 3 years ago

simplified version:

var destroyAll = `
DROP DATABASE dbName;
DROP USER userName;
`

db.MustExec(destroyAll)

panic: pq: DROP DATABASE cannot run inside a transaction block

goroutine 1 [running]:
github.com/jmoiron/sqlx.MustExec(...)
    /Users/mj/go/pkg/mod/github.com/jmoiron/sqlx@v1.2.0/sqlx.go:718
github.com/jmoiron/sqlx.(*DB).MustExec(0xc00007eba0, 0xc0000be050, 0x45, 0x0, 0x0, 0x0, 0x0, 0x0)
    /Users/mj/go/pkg/mod/github.com/jmoiron/sqlx@v1.2.0/sqlx.go:370 +0xb7
main.main()
    /tmp/qaxaw/main.go:71 +0x83b
exit status 2

when i try to drop a database and user in one sql, i got this error, i have to split the sql to make it work.

erichanson commented 3 years ago

Yes. This is how PostgreSQL works, not an issue with this library.

gshilin-sdb commented 3 years ago

The issue is: how to run db.Exec without transaction block?

gshilin-sdb commented 3 years ago

And the answer is: use two Execs:

var destroyAll1 = `
DROP DATABASE dbName;
`
var destroyAll2 = `
DROP USER userName;
`

db.MustExec(destroyAll1)
db.MustExec(destroyAll2)