This is the case: the_id of Table B in Table Aidint (11) Ba_idint (11) is the foreign key of Table A's primary key ----------- the division line ------------ I started mysql, then, an operation needs to insert A piece of data to Table A (hereinafter referred to as operation 1) before inserting data... the situation is as follows:
Table
Id int (11)
Table B
A_id int (11)
The a_id of Table B is the foreign key of Table A's primary key.
----------- The dividing line of all evil ------------
I enabledmysql
And then insert a data entry toTable
(Hereinafter referred toOperation 1
), And then insert the dataTable B
(Hereinafter referred toOperation 2
).
Operation 2 depends on the ID returned after operation 1 is successfully inserted
When a transaction is enabled, after the operation is successful, data is inserted into Table B. At this time, the foreign key is detected. Although the operation is successful, but the transaction has not been submitted, so there is no such data in Table A, so operation 2 will report an error...
How do you solve this problem (my current solution is to cancel the foreign key index !!!)?
Reply content:
The situation is as follows:
Table
Id int (11)
Table B
A_id int (11)
The a_id of Table B is the foreign key of Table A's primary key.
----------- The dividing line of all evil ------------
I enabledmysql
And then insert a data entry toTable
(Hereinafter referred toOperation 1
), And then insert the dataTable B
(Hereinafter referred toOperation 2
).
Operation 2 depends on the ID returned after operation 1 is successfully inserted
When a transaction is enabled, after the operation is successful, data is inserted into Table B. At this time, the foreign key is detected. Although the operation is successful, but the transaction has not been submitted, so there is no such data in Table A, so operation 2 will report an error...
How do you solve this problem (my current solution is to cancel the foreign key index !!!)?
I tried it. Operation 2 will not report an error. Look for another reason.
You can start two transactions for each of the two operations.