You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, sorry if my english is not perfect, I'll try my best to be as understandable as possible.
My issue is the following, in my User model, I have a @afterCreate() that creates a Token, this token is sent by email to the user so that he can activate his account.
This worked perfectly until I introduced db transactions in the process.
Now my User is not created anymore by a simple User.create(), it is created in a transaction.
I need a transaction because when a user registers, i need to create multiple data in different tables and I wrapped this in a transaction so that i'm sure everything works well before the data is written in the database.
Now I added the transaction, the hook @afterCreate() throw an errror insert or update on table "tokens" violates foreign key constraint "tokens_user_id_foreign"
If I activate the debug so that I can see the SQL commands executed, I can see this:
We can see that the user is created, then the token, and then the await token.related('user').associate(user) tries to update the token to specify the user related to this token but it fails.
If i create the user outside a transaction, the issue disappear.
Thank you for your quick response! It solves my problem, thank you for your help.
And what happens now if the hook is call by a User creation but not in a transaction? The user.$trx might be undefined
Yes and that is not the problem. What was happening earlier was, you were trying to create the token outside of the transaction and assigning to the user within the transaction.
Another way to fix this is to use the user -> hasMany -> tokens relationship for persistence and then you won't need this user.$trx assignment. Something like
Package version
21.3.0
Describe the bug
Hello,
First of all, sorry if my english is not perfect, I'll try my best to be as understandable as possible.
My issue is the following, in my User model, I have a
@afterCreate()
that creates a Token, this token is sent by email to the user so that he can activate his account.This worked perfectly until I introduced db transactions in the process.
Now my User is not created anymore by a simple
User.create()
, it is created in a transaction.I need a transaction because when a user registers, i need to create multiple data in different tables and I wrapped this in a transaction so that i'm sure everything works well before the data is written in the database.
Now I added the transaction, the hook
@afterCreate()
throw an errrorinsert or update on table "tokens" violates foreign key constraint "tokens_user_id_foreign"
If I activate the debug so that I can see the SQL commands executed, I can see this:
We can see that the user is created, then the token, and then the
await token.related('user').associate(user)
tries to update the token to specify the user related to this token but it fails.If i create the user outside a transaction, the issue disappear.
Here is an excerpt of my code
@afterCreate()
:Maybe I just did something wrong, please let me know if so. Or maybe it's a bug and i'll be happy to help to solve this.
Thank you
Reproduction repo
No response
The text was updated successfully, but these errors were encountered: