英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论