GORM Playground Link

Description

之前在select里加上w.id 就能生成count(*),但是更新框架后就报错了

$A07QT)8W)A4C5Y38I__UHK

%6`UN%P}N_Q5(ZD~)60X$CT

如果不加w.id 还是会报错 ~R@{HDT@FN_925@MRLSAJF0

解决方法是select放在count之后, 这也太反人类了. 4}OYU@${KCUKK{2QK GIJQT

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

After a Chain method, Finisher Method, GORM returns an initialized gorm.DB instance, which is NOT safe to reuse anymore, you should use a New Session Method to mark the gorm.DB as shareabl

https://gorm.io/docs/method_chaining.html#New-Session-Method

Comment From: tangqiu0205

用了Session 还是不行, Select 必须放在Count 后面才行吗? Gorm 连表统计总数,生成sql报错.

Comment From: tangqiu0205

连表查询如果不写select, find或者scan在两张表内有相同字段情况下能正确赋值吗,

Comment From: a631807682

Please take a look at the code example and the code comments explaining why this happens and how to fix it.

tx := db.Where("name = ?", "jinzhu").Session(&gorm.Session{})

// good case
tx.Where("age = ?", 18).Find(&users)
// SELECT * FROM users WHERE name = 'jinzhu' AND age = 18

Comment From: tangqiu0205

我就是照着这个地方改的. 1:先拼接where条件 2:使用session让其可复用 3:查询s表数据 4:统计总数 哪里出现问题了?

Comment From: a631807682

db = db.Session(&gorm.Session{})