go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.81k stars 3.93k forks source link

connection 方法在阿里云 PolarDB 中无效 #5879

Closed zwjzxh520 closed 1 year ago

zwjzxh520 commented 1 year ago

代码如下

    dsn := "xxx:xxxx@tcp(xxxx)/xxx?charset=utf8mb4&parseTime=True&loc=Local"
    db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
    if err != nil {
        log.Fatalf("connect db get error: %v", err)
        return
    }
    srcDb, err := db.DB()
    srcDb.SetMaxIdleConns(1)
    srcDb.SetMaxOpenConns(1)
    srcDb.SetConnMaxLifetime(time.Hour)

    db.Connection(func(dbs *gorm.DB) error {

        if result := dbs.Exec("create temporary table temp_order_weixin as select * from `order` where bank = 'weixin' AND id > 1"); result.Error != nil {
            log.Fatalf("create temp table get error: %v", result.Error)
            return result.Error
        }

        var count int64
        if result := dbs.Table("temp_order_weixin as o").
            Select("count(o.id)").
            Where("o.id > ?", 1).
            Count(&count); result.Error != nil {
            log.Fatalf("count get error: %v", result.Error)
            return result.Error
        }
        return nil
    })

运行日志

2022/11/25 17:50:24 /demo/demo.go:120 SLOW SQL >= 200ms
[798.551ms] [rows:64350] create temporary table temp_order_weixin as select * from `order` where bank = 'weixin' AND id > 1

2022/11/25 17:50:24 /demo/demo.go:131 Error 1146: Table 'vip_paygateway.temp_order_weixin' doesn't exist
[101.445ms] [rows:64350] SELECT count(o.id) FROM temp_order_weixin as o WHERE  o.id > 1
2022/11/25 17:50:24 count get error: Error 1146: Table 'vip_paygateway.temp_order_weixin' doesn't exist

在阿里云普通的 RDS 中不会报此错误。

https://github.com/go-gorm/playground/pull/545

github-actions[bot] commented 1 year ago

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

li-jin-gou commented 1 year ago

This seems to be a database problem.😁

zwjzxh520 commented 1 year ago

应该不是的。 我发现有个情况: Raw() 方法,如果不传第二个参数,则能正常执行。只要传了第二个参数,就会报这个问题