I'm facing a problem for a project I'm working on.
I have a MySQL database that I use with a Node.js script (thanks to the mysql npm package).
I have two entities, A and B, with a many-to-many relationship. Between them, there's a associative entity table, named Map_A_B. Foreign-keys are defined between both A and Map_A_B, and B and Map_A_B.
Here is how it looks like:
The problem I'm facing is when I'm trying to insert data (multiple rows). Because I'm doing an INSERT of many rows, then I don't have the id for each table and can't fill the mapping table (let's say that each A contains 100 B's).
Before, instead of having an associative entity table, I just had a foreign-key inside of B that helps me to store the Aid, and this was pretty easy to do. But now, I can't do this anymore.
I've done some research before, using JOIN I can SELECT / DELETE without any problem, I'm just a bit lost about this multiple row INSERT...
Do you have an idea about how to solve this problem?
After inserting a row, ask for its id via
LAST_INSERT_ID()
.