I got two SQLite databases which both have junction tables to describe the one-to-many relationship. Now these two DB need to be merged into a single one with some kind of import/export mechanism and still keep the relationships.
I tried to dump DB2 with .dump
and then load it back in DB1 with .read
, but always got PRIMARY KEY must be unique
warnings.
Is there any best practice to handle this kind of situation?
Preferred not to use attach
to avoid extra complexity.
DB1
Fruit
-------------- | id | name | -------------- | 1 | Apple | | 2 | Lemon | | 3 | Kiwi | --------------
Juice
---------------- | id | name | ---------------- | 1 | Juice A | | 2 | Juice B | ----------------
Recipe (Junction Table)
---------------------------- | id | juice_id | fruit_id | ---------------------------- | 1 | 1 | 1 | | 2 | 1 | 2 | | 3 | 2 | 1 | | 4 | 2 | 3 | ----------------------------
DB2
Fruit
--------------- | id | name | --------------- | 1 | Kiwi | | 2 | Lemon | | 3 | Apple | | 4 | Orange | ---------------
Juice
---------------- | id | name | ---------------- | 1 | Juice C | | 2 | Juice D | ----------------
Recipe (Junction Table)
---------------------------- | id | juice_id | fruit_id | ---------------------------- | 1 | 1 | 1 | | 2 | 1 | 3 | | 3 | 2 | 2 | | 4 | 2 | 4 | ----------------------------