Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gorm 二期 升级后使用map添加报错 LastInsertId is not supported by this driver,同时如果有count()查询时候生成的update语句异常 #708

Open
943885179 opened this issue Mar 28, 2024 · 0 comments
Assignees

Comments

@943885179
Copy link

原来使用正常,版本为
gorm.io/gorm v1.23.8
gorm.io/driver/postgres v1.3.9
升级后
gorm.io/gorm v1.25.8
gorm.io/driver/postgres v1.5.7

问题一:无法通过map添加数据

error:LastInsertId is not supported by this driver,无法插入数据

以下为测试代码

package gorm

import (
	"fmt"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"testing"
)

func TestRun(t *testing.T) {
	var source = `host=127.0.0.1 user=postgres password=postgres dbname=ztf_host port=5432  sslmode=disable`
	//tdb, err := NewGorm(&models.DbConfig{
	//	DbType: models.DbType(1),
	//	Source: source,
	//})
	tdb, err := gorm.Open(postgres.Open(source), &gorm.Config{})
	if err != nil {
		return
	}
	var c int64
	//dba := tdb.Table("test").Debug().Where(fmt.Sprintf(`%s =?`, "a"), "111")
	//dba.Count(&c)
	var newData = map[string]any{
		"a":  "222",
		"id": "1",
	}
	err = tdb.Table("test").Debug().Create(&newData).Error
	fmt.Println(err)
	fmt.Println(c)
}

问题二:gorm如果有求数量操作的话,生成的更新语句错误
此时生成的sql为 update test set a='222' from test where a='111'

package gorm

import (
	"fmt"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"testing"
)

func TestRun(t *testing.T) {
	var source = `host=127.0.0.1 user=postgres password=postgres dbname=ztf_host port=5432  sslmode=disable`
	//tdb, err := NewGorm(&models.DbConfig{
	//	DbType: models.DbType(1),
	//	Source: source,
	//})
	tdb, err := gorm.Open(postgres.Open(source), &gorm.Config{})
	if err != nil {
		return
	}
	var c int64
	dba := tdb.Table("test").Debug().Where(fmt.Sprintf(`%s =?`, "a"), "111")
	dba.Count(&c)
	var newData = map[string]any{
		"a":  "222",
	}
	err =dba.Updates(&newData).Error
	fmt.Println(err)
	fmt.Println(c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants