Skip to content

Raw("SELECT currentDatabase()") incorrectly triggers "invalid INSERT query" — breaks GORM AutoMigrate #222

@FairyTale5571

Description

@FairyTale5571

When using gorm.io/driver/clickhouse together with clickhouse-go/v2, running the following query:

db.Raw("SELECT currentDatabase()").Scan(&name)
results in this error:

invalid INSERT query: SELECT currentDatabase()
This is unexpected because the query is clearly a SELECT, not an INSERT.

Expected behavior
The query SELECT currentDatabase() should execute without error and return the current database name.

Actual behavior
The query is incorrectly classified as an INSERT and fails with:

invalid INSERT query: SELECT currentDatabase()

Reproduction steps

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/ClickHouse/clickhouse-go/v2"
)

func main() {
    dsn := "clickhouse://default:@localhost:9000/default"
    db, err := sql.Open("clickhouse", dsn)
    if err != nil {
        panic(err)
    }

    query := "SELECT currentDatabase()"
    var dbName string
    if err := db.QueryRow(query).Scan(&dbName); err != nil {
        panic(fmt.Errorf("error: %w", err))
    }

    fmt.Println("Current DB:", dbName)
}

Works with sql.DB().QueryRow(...)
Fails with GORM's db.Raw(...).Scan(...)

Environment
Go version: go1.23.8

clickhouse-go version: v2.37.2

GORM version: v1.30.0

OS: macOS

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions