GORM将查询文本转换为小写。

huangapple go评论84阅读模式
英文:

GORM converts query text into lowercase

问题

你可以在定义模型结构体时使用gorm:"column:createdAt"标签来指定列名为"createdAt",这样GORM就不会将其转换为小写。修改后的代码如下所示:

type Order struct {
    // other fields...
    CreatedAt time.Time `gorm:"column:createdAt"`
}

这样,当你执行查询时,GORM 将使用指定的列名"createdAt"而不是转换为小写的"createdat"。

英文:
response := db.Where("createdAt BETWEEN ? AND ?", today, tomorrow).Find(&orders)

I want to make a query with GORM. The column in my table is named "createdAt" but GORM converts it to. "createdat"

    ERROR: column "createdat" does not exist (SQLSTATE 42703)
[172.521ms] [rows:0] SELECT * FROM "Orders" WHERE `createdAt` BETWEEN '2021-04-21 04:00:00' AND '2021-04-22 04:00:00'

How can I prevent GORM from converting the query text to lowercase?

答案1

得分: 2

如果您使用区分大小写的列名,请使用\",例如:\"createdAt\"。现代数据库都无法处理大小写文本。

英文:

If you use case sensitive column name, use \",ex: \"createdAt\", No modern database cannot handle upper or lower case text.

答案2

得分: 1

这不是gorm的问题,而是数据库的问题,意思是在数据库中创建了一个名为created_at的列,请检查一下,并尝试用以下代码替换你的代码:

response := db.Where("created_at BETWEEN ? AND ?", today, tomorrow).Find(&orders)
英文:

Its not gorm's issue its database issue means in database column is created with name created_at just check there and try replacing your code with this

response := db.Where("created_at BETWEEN ? AND ?", today, tomorrow).Find(&orders)

huangapple
  • 本文由 发表于 2021年10月10日 05:58:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/69510834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定