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

Clauses(clause.OnConflict{UpdateAll: true,}). The generated SQL is abnormal. The fields column_a and column_b are missing after ON DUPLICATE KEY UPDATE. #7396

Open
dackh opened this issue Mar 19, 2025 · 1 comment
Assignees
Labels

Comments

@dackh
Copy link

dackh commented Mar 19, 2025

GORM Playground Link

code

package main

import (
	"fmt"
	"log"

	"github.com/shopspring/decimal"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/logger"
)

/*
CREATE TABLE `test_table` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `column_a` decimal(23,6) NOT NULL DEFAULT '0.000000' COMMENT 'a',
  `column_b` decimal(23,6) NOT NULL DEFAULT '0.000000' COMMENT 'b',
  `column_c` decimal(23,6) DEFAULT NULL COMMENT 'c',
  `column_d` decimal(23,6) DEFAULT NULL COMMENT 'd',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
*/

type TestTable struct {
	//
	Id int64 `gorm:"column:id;type:bigint(20) AUTO_INCREMENT;primaryKey;precision:19;scale:0;not null;autoIncrement"`
	// a
	ColumnA decimal.Decimal `gorm:"column:column_a;type:decimal(23,6);default:0.000000;precision:23;scale:6;not null"`
	// b
	ColumnB decimal.Decimal `gorm:"column:column_b;type:decimal(23,6);default:0.000000;precision:23;scale:6;not null"`
	// c
	ColumnC decimal.NullDecimal `gorm:"column:column_c;type:decimal(23,6);precision:23;scale:6"`
	// d
	ColumnD decimal.NullDecimal `gorm:"column:column_d;type:decimal(23,6);precision:23;scale:6"`
}

func main() {
	// 数据库连接配置
	dsn := "username:password@tcp(127.0.0.1:3306)/test_db?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		log.Fatalf("Failed to connect to database: %v", err)
	}

	err = db.AutoMigrate(&TestTable{})
	if err != nil {
		log.Fatalf("Failed to auto migrate: %v", err)
	}

	products := []TestTable{
		{Id: 1, ColumnA: decimal.NewFromFloat(19.99),
			ColumnB: decimal.NewFromFloat(19.99),
			ColumnC: decimal.NullDecimal{Decimal: decimal.NewFromFloat(19.99), Valid: true},
			ColumnD: decimal.NullDecimal{Decimal: decimal.NewFromFloat(19.99), Valid: true},
		},
	}

	result := db.Table("test_table").Clauses(clause.OnConflict{
		UpdateAll: true,
	}).Create(&products)

	if result.Error != nil {
		log.Fatalf("Failed to upsert products: %v", result.Error)
	}

	fmt.Println("Upserted products successfully!")
}

Description

gorm : 1.25.5

Execute the generated SQL

[3.089ms] [rows:1] INSERT INTO `test_table` (`column_c`,`column_d`,`column_b`,`id`,`column_a`) VALUES ('19.99','19.99','19.99',1,'19.99') ON DUPLICATE KEY UPDATE `column_c`=VALUES(`column_c`),`column_d`=VALUES(`column_d`)
Upserted products successfully!
@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Mar 19, 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

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