看了文档没有找到怎么使用表别名,貌似能做的只有 Table("t1 as a"), 然而这个用法一旦与 First / Last 一起用就会出错。 不知道 gorm 是不是就完全没考虑过别名的支持,还是说有什么 API 我不知道的
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: li-jin-gou
你好 @x-xy-y 我这里是使用 gorm v2 运行了一下,没什么问题~ 代码:
func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
DB.Create(&user)
var result User
if err := DB.Table("users as u").First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
}
Result:
[7.379ms] [rows:1] INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`name`,`age`) VALUES ('2022-02-22 16:52:48.39','2022-02-22 16:52:48.39',NULL,'jinzhu',0)
[2.913ms] [rows:1] SELECT * FROM users as u WHERE `u`.`id` = 1 AND `u`.`deleted_at` IS NULL ORDER BY `u`.`id` LIMIT 1
可以在 https://github.com/go-gorm/playground/ 提供一个可复现的demo嘛?
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: x-xy-y
哦,试了一下,跟版本有关系。v2 的没问题,v1 的有。
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 ✨