GORM Playground Link
Sorry, no playground pull request here, but i will create a PR when this bug is confirm as a bug, not a wrong usage.
Description
When I use the Unscoped() function, the parsed sql seems to have serious errors, I'm not sure if this is a bug, or my wrong use? If it's my problem, should there be a little hint in the documentation?
error code below:
db := data.GetDB().Unscoped() // get a instance of *gorm.DB
subQuery := db.Model(table.User{}).Select("user_id").Where(table.User{EnterpriseId: "xxx"})
var deletes []table.UserRelation
// completely wrong sql here:
// DELETE FROM "user" WHERE "user"."enterprise_id" = 'xxx' AND user_id IN (DELETE FROM "user" WHERE "user"."enterprise_id" = 'xxx' AND user_id IN ()
if err := db.Where("user_id IN (?)", subQuery).Delete(&deletes).Error; err != nil {
return err
}
return nil
correct usage below:
db := data.GetDB() // get a instance of *gorm.DB
subQuery := db.Unscoped().Model(table.User{}).Select("user_id").Where(table.User{EnterpriseId: "xxx"})
var deletes []table.UserRelation
// correct sql here:
// DELETE FROM "user_relation" WHERE user_id IN (SELECT "user_id" FROM "user" WHERE "user"."enterprise_id" = 'xxx')
if err := db.Unscoped().Where("user_id IN (?)", subQuery).Delete(&deletes).Error; err != nil {
return err
}
return nil
Looking forward to reply and thanks in advance!
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 shareable.
https://gorm.io/docs/method_chaining.html#New-Session-Method
Comment From: axiangcoding
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 shareable.
https://gorm.io/docs/method_chaining.html#New-Session-Method
Thanks, it explain why. And maybe add the full list to the document is a good idea? not a link to github sourcecode?
Comment From: a631807682
We have not introduce Chain Method and Finisher Method separately, because they are often not used alone, welcome to help us improve the document.
https://github.com/go-gorm/gorm.io
Comment From: axiangcoding
We have not introduce
Chain MethodandFinisher Methodseparately, because they are often not used alone, welcome to help us improve the document. https://github.com/go-gorm/gorm.io
I'll add some hints and translate that page into Chinese, thanks again.