pg icon indicating copy to clipboard operation
pg copied to clipboard

Access to pg.Tx or pg.DB inside hooks

Open jpascal opened this issue 5 years ago • 2 comments

Steps:

  1. I have open transaction
  2. Create table inside transaction
  3. 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.

jpascal avatar May 03 '20 15:05 jpascal

No one has this issue?

jpascal avatar May 15 '20 05:05 jpascal

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

ernsheong avatar Jun 12 '20 03:06 ernsheong