Skip to content

Relation methods are not using options.client when wrapping in managedTransaction #1095

Open
@adamcikado

Description

@adamcikado

Package version

21.6.1

Describe the bug

If you provide transaction when calling any of the relation methods like updateOrCreate it won't use it when calling managedTransaction, which results in creating another connection with another transaction.

class Post extends BaseModel {
  @column({ isPrimary: true })
  declare id: number

  @column()
  declare userId: number

  @column()
  declare title: string
}

class User extends BaseModel {
  @column({ isPrimary: true })
  declare id: number

  @column()
  declare username: string

  @hasMany(() => Post)
  declare posts: HasMany<typeof Post>
}

const user = new User()
user.username = 'virk'
await user.save()

const trx = await db.connection().transaction()
await user.related('posts').updateOrCreate({}, { title: 'updateOrCreate' }, { client: trx })
await trx.commit()

Run the command with DEBUG=knex:* and notice that 2 transactions are created.

Reproduction repo

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions