go-gorm / gorm

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

sql syntax error #5580

Closed fun-li closed 2 years ago

fun-li commented 2 years ago

When I use gorm, the following sql statement is generated, it cannot be executed normally, but it is executed normally in the mysql client. At first, it is suspected that it is a database-driven problem. Use github.com/go-sql-driver/mysql driver It can be executed normally, I want to ask if it is a problem with gorm.


type VerificationSource struct {
    BaseModel
    Name         string                              `json:"name,omitempty"`
    Resource     string                              `json:"resource,omitempty"`
    ResourceType verificationenum.VerificationSource `json:"resourceType,omitempty" gorm:"column:type"`
}
type BaseModel struct {
    ID         uint                  `gorm:"primaryKey;comment:id" json:"id,omitempty"`
    CreateBy   string                `json:"createBy"`
    UpdateBy   string                `json:"updateBy"`
    CreateTime time.Time             `gorm:"comment:创建时间;autoCreateTime" json:"createTime"` // 在创建时,如果该字段值为零值,则使用当前时间填充
    UpdateTime time.Time             `gorm:"comment:更新时间;autoCreateTime" json:"updateTime"` // 在创建时该字段值为零值或者在更新时,使用当前时间戳秒数填充
    DeleteFlag soft_delete.DeletedAt `gorm:"softDelete:flag;DeletedAtField:delete_flag" json:"-"`
}
//grom 
sources := make([]model.VerificationSource, 0)
_ = ver.Select("resource").Where("resource is not ? ", nil).Find(sources)
//generated sql :SELECT resource FROM `li_verification_source` WHERE resource is not NULL  AND `li_verification_source`.`delete_flag` = 0

Error message :Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? AND li_verification_source.delete_flag = ?' at line 1

github-actions[bot] commented 2 years 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

a631807682 commented 2 years ago

This is obviously a problematic prepare statement, which cannot be created successfully, not only on https://github.com/go-sql-driver/mysql, but also on mysql itself.

SELECT resource FROM verification_sources WHERE resource is not ?