GORM Playground Link

https://github.com/go-gorm/playground/pull/119

Description

See pull request. Added bug description into the code.

Comment From: jinzhu

Hi @Techassi

Seems no issue in your tests, but I think you might got this issue, check out the breaking changes

https://gorm.io/docs/v2_release_note.html#Method-Chain-Safety-Goroutine-Safety

Comment From: Techassi

Noted!

I already thought about the new re-use system. So thank you for pointing me to the right direction and confirm my suspection.

Comment From: Techassi

Hi @jinzhu

To be perfectly honest this change makes my life so much more harder. Take this simple scenario for example:

  1. A new user of some service wants to register to said service, it fails with the error Error 1062: Duplicate entry 'Test' for key 'PRIMARY' because the username already exists (For this example we will use the username Test)
  2. The user with the username Test wants to login, it fails with the same error as above (Error 1062: Duplicate entry 'Test' for key 'PRIMARY') because of statement re-use.

So basically I am forced to make all by DB calls use WithContext() like

err := db.WithContext(context.Background()).Where("username = ?", username).First(user).Error

or

err := db.WithContext(context.Background()).Create(user).Error

I don't feel like this is either intended or a very programmer-firendly way to make DB queries. I look forward to your opinion @jinzhu

Comment From: Techassi

Any update on this @jinzhu ?

Comment From: jinzhu

it is clearly described and there is a detailed example in the document.

You don't always need a WithContext method but only when you are sharing your db conditions, which is not a common case in your whole application.

Comment From: Techassi

Thank you @jinzhu

Well I read through the linked document. Still the above example should NOT fail. I am NOT sharing any conditions and still the returned error persists across two totally different queries. Is this intended behaviour?

Can you please comment on the above example and why the error persists across multiple database calls.

Edit: I will continue using WithContext() because without the most basic queries break.

Comment From: jinzhu

can you make the tests run & fail?