'Updating table data from triggers
MySQL Version 8.0
I am attempting to create a chain of triggers to update information based on data inserted into other, down stream, tables.
I am running into problems though.
Scenario:
I want to insert data into Table B after a record is inserted into Table A.
I have a trigger on After Insert that does this.
Table B inserts the data from the After Insert trigger on Table A and creates a new record.
Now, on After Insert on Table B, I want to take the ID created and update the newly inserted record in Table A with that value.
Actual Use-case:
I am creating a Discord bot that will run a game. When a user creates a character, I want the MySql server to create an inventory for that player's character.
Once this happens, I want it to then update that player's character record in the Characters table (Table A) with the new inventory id generated by the Inventory table (Table B).
The Issue(s):
This process triggered the "You can't call Insert/Update/Delete on object that initiated the transaction' error. I get why they would have this for Table A After Insert to an insert statement on a trigger in Table B to insert data back into A, but it makes no sense to me why they have it when we're going from insert to an update which should be separated.
Does MySql combine all table operations done from triggers like this into the same transaction to increase performance or something? Imo it does the opposite.
Next I tried using a stored procedure, but apparently we can't create a transaction within a SP called from a trigger. Upon further reading on Google, we cannot nest transactions. Once removing the transaction I get an error similar to the one from the triggers about not being able to update after insert. Its a pain in the ass.
Am I going to be stuck with having to make multiple distinct calls from code to do what I want to in the use case?
Seems very backwards and system resource heavy.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
