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

Scan结果映射错误 #7400

Open
zzuts opened this issue Mar 26, 2025 · 2 comments
Open

Scan结果映射错误 #7400

zzuts opened this issue Mar 26, 2025 · 2 comments
Assignees
Labels

Comments

@zzuts
Copy link

zzuts commented Mar 26, 2025

执行 insert on duplicate key update price = price + 0 之后,再执行Scan()方法,结果映射错误。会直接扫描数据库全部记录

CREATE TABLE `products` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `deleted_at` datetime(3) DEFAULT NULL,
  `code` varchar(255) DEFAULT NULL,
  `price` bigint DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_code` (`code`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

先创建两条记录

db.Create(&Product{Code: "D41", Price: 100})
db.Create(&Product{Code: "D42", Price: 100})

执行insert on duplicate key update语句

dst := &Product{
		Code:  "D42",
		Price: 0,
	}
tx := db.Begin()
	tx = tx.Table(dst.Name()).Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "code"}}, DoUpdates: clause.Assignments(map[string]interface{}{"price": gorm.Expr("price + ?", dst.Price)})}).Create(dst)
	p := make([]Product, 0, 10)
	tx.Scan(&p)
fmt.Printf("tx scan ||%+v\n", p)
	tx.Commit()

如果on duplicate key update 执行的是 price = price+ 0 这样无改动的语句时,后面再执行Scan。会把sql中所有记录扫出来,Debug()一下,发现执行的是SELECT * FROMproductsWHEREproducts.deleted_at IS NULL

Image
Image

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Mar 26, 2025
Copy link

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

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants