volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

Revert transaction in hook #574

Closed Napas closed 5 years ago

Napas commented 5 years ago

Hi,

I need to execute a couple more inserts/updates in the hook beforeInsert or afterInsert hook.

Is there a way to wrap everything in the single transaction, so if any of the queries (including model insert query) fails - all other will be reverted?

aarondl commented 5 years ago

The hooks are passed the same database handle as you pass in to the method itself. https://github.com/volatiletech/sqlboiler/blob/master/templates/15_insert.go.tpl#L44-L46

So if you pass in a transaction to the Insert then that same transaction is used for the hooks as well.