I have two tables. The second has an FK back to the first. This is a one to many relationship.
Body:
| body_id | body | add_date |
|---|---|---|
| 1 | alien butts drive me nuts | 2022-01-01 |
Body_Tags:
| body_tag_id | tag_id | body_id |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 5 | 1 |
| 3 | 8 | 1 |
I have an xlsx file that has these columns;
| body | add_date | tag1 | tag2 | tag3 |
|---|
I want to use Pandas and SQLAlchemy to insert the first two columns into Body as one row. Then insert tag1, tag2, tag3 into the Tags table all as their own row - each having the id from the Body table.
I have SQLAlchemy data models that match my database and I can query using the models and my FK and relationships work too. I can use Pandas to pull the xlsx file into a dataframe. I just don't know how I would insert a one to many like this. to_sql() inserts an entire dataframe at once. So getting the corresponding IDs wouldn't work with to_sql as far as I know.
Any way to achieve my goal?