adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.07k stars 192 forks source link

Using transactions with relationships #83

Closed almightyju closed 7 years ago

almightyju commented 7 years ago

I tried to use some code similar to this:

const user = new User(values)
user.useTransaction(trx)
yield user.save()
yield user.configs().attach([1,2,3])

but it hangs on the attach. Folling the code through its becase in Lucid/Relations/BelongsToMany.js attach runs this code:

yield this.relatedQuery.queryBuilder.table(this.pivotTable).insert(values)

which doesn't have any transaction logic, it would be nice if I could run this code:

const user = new User(values)
user.useTransaction(trx)
yield user.save()
yield user.configs().useTransaction(trx).attach([1,2,3])
thetutlage commented 7 years ago

Yup, will fix this behavior

thetutlage commented 7 years ago

Fixed in 4.0. You can pass the trx object to the save, create, attach methods manually now.

RauppRafael commented 6 years ago

@thetutlage in case i have the following code, how do I make it part of a transaction? It's not very clear in the docs when saving relations...

await match.players().save(user)

I tried:

await match.players().save(user, tx)

But doesn't seem to work.

Dimona commented 6 years ago

I have the same problem Tried different ways

const partner = await Partner.create(data, tx);
await client.partners().save(partner, tx);

or

const partner = await client.partners().create(data, tx);

or

const partner = await Partner.create(data, tx);
await client.partners().attach([partner.id], null, tx);
xaddict commented 5 years ago

This problem still exists.

When I create an account, a group and a user there's no easy way to add it as a transaction

jonaselan commented 4 years ago

The only one I found to do that was:

await Partner.query()
      .where(user_id, 1)
      .transacting(transaction)
      .update({name : 'john'})
un-versed commented 3 years ago

Any answer to this?

Gredys commented 1 month ago

Its 2024 and still broken?