pg
pg copied to clipboard
Access to pg.Tx or pg.DB inside hooks
Steps:
- I have open transaction
- Create table inside transaction
- Insert model in table
3.1. BeforeInsert
3.1.1. Run db.Model(&User{}).Where(
"users"."id" != ? AND "users"."email" = ?, u.ID, u.Email).Count() -> ERROR #42P01 relation "users" does not exist
Error will apper because I cant run new query inside opened transaction.
How I can fix it? I dont want use context, because context don't know about transaction.
No one has this issue?
Related: https://github.com/go-pg/pg/issues/1294
Author does not want db in hooks (opinionated), I hope that this can be reconsidered @vmihailenco
Frankly I think hooks are pointless if they can't access the DB easily. Pumping in db back into Context is kinda ugly, but I guess seeing this issue it gives you the flexibility to choose either a DB or a Tx