Transaction bug on related data

Hello. I think this is a bug.

Please review following code.

await Database.transaction(async (trx) => {

  const item = new UserItem()
  item.user_id = 666
  await item.save(trx)

  const query = await User
    .transacting(trx)
    .with('items')
    .where('id', 666)
    .firstOrFail()
  console.log(query.toJSON())
  // The result like;
  // 
  // { "id": 666, items: [] }

  const items = await UserItem
    .transacting(trx)
    .where('user_id', 666)
    .fetch()
  console.log(items.toJSON())
  // The result like;
  // 
  // [ { "id", 1, "user_id", 666 } ]
})

If I insert a record to a table via transaction, I can’t see that record via .with() method even I add transactiing method to chain.

But if I query it directly with transaction, I can see it.

I think that, in .with method, I can’t use transactions.

1 Like

Hi!

Not really sure but try to add transaction to with() query too

  const query = await User
    .transacting(trx)
    .with('items', (builder) => {
      builder.transacting(trx)
    })
    .where('id', 666)
    .firstOrFail()