GORM Playground Link

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

Description

// gorm_test.go
package test

import (
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
    "testing"
)

type BaseModel struct {
    CreatedAt int64          `gorm:"autoCreateTime:nano"`
    UpdatedAt int64          `gorm:"autoUpdateTime:nano"`
    DeletedAt gorm.DeletedAt `gorm:"index"`
}

type UUIDModel struct {
    BaseModel
    ID string `gorm:"primarykey;type:varchar(36);not null;uniqueIndex;comment:主键ID;colum:id"`
}

type User struct {
    UUIDModel
    Username string `gorm:"type:varchar(32);not null;unique;comment:用户名"`
    Password string `gorm:"type:varchar(255);not null;comment:密码"`
    Email    string `gorm:"type:varchar(64);not null;unique;comment:邮箱"`
    Phone    string `gorm:"type:varchar(16);not null;unique;comment:手机号"`
    Avatar   string `gorm:"type:varchar(255);not null;comment:头像"`
    Nickname string `gorm:"type:varchar(32);not null;comment:昵称"`
}

const dsn = "root:<password>@tcp(<ip>:<port>)/<db_name>?charset=utf8mb4&parseTime=True&loc=Local"

var db *gorm.DB

func init() {
    conn, err := gorm.Open(mysql.Open(dsn))
    if err != nil {
        panic(err)
    }
    db = conn

    // 初始化表
    //if err = db.AutoMigrate(&User{}); err != nil {
    //  panic(err)
    //}
}

func TestGormP(t *testing.T) {
    uuidPK := "257b218c-5031-45a1-8061-209180499a93"
    var user User
    var sql string
    sql = db.ToSQL(func(tx *gorm.DB) *gorm.DB {
        return tx.Model(&user).First(&user, uuidPK)
    })
    t.Log(sql)

    sql = db.ToSQL(func(tx *gorm.DB) *gorm.DB {
        return tx.Model(&user).First(&User{}, []string{uuidPK})
    })

    t.Log(sql)

    sql = db.ToSQL(func(tx *gorm.DB) *gorm.DB {
        return tx.Model(&user).Delete(&user, uuidPK)
    })

    t.Log(sql)

    sql = db.ToSQL(func(tx *gorm.DB) *gorm.DB {
        return tx.Model(&user).Delete(&user, []string{uuidPK})
    })

    t.Log(sql)
}

控制台输出

    gorm_plus_test.go:54: SELECT * FROM `users` WHERE 257b218c-5031-45a1-8061-209180499a93 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
    gorm_plus_test.go:60: SELECT * FROM `users` WHERE `users`.`id` = '257b218c-5031-45a1-8061-209180499a93' AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
    gorm_plus_test.go:66: UPDATE `users` SET `deleted_at`='2023-01-16 11:04:36.896' WHERE 257b218c-5031-45a1-8061-209180499a93 AND `users`.`deleted_at` IS NULL
    gorm_plus_test.go:72: UPDATE `users` SET `deleted_at`='2023-01-16 11:04:36.896' WHERE `users`.`id` = '257b218c-5031-45a1-8061-209180499a93' AND `users`.`deleted_at` IS NULL

In this example, you can see that when the First and Delete methods are executed, the sql generation error occurs if the second argument is a uuid, which is not a number string

Comment From: github-actions[bot]

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.io ✨ Search Before Asking

Comment From: a631807682

https://gorm.io/docs/query.html#Retrieving-objects-with-primary-key

When the type is string, we cannot actually confirm that the user wants to query through the primary key

db.First(&user, "1=1")