Search code examples
postgresqlimportdatabase-migrationprismaprisma-graphql

prisma import fails to import relation tables with error: Failure inserting into relationtable


When trying to import data into a postgres database with prisma CLI's prisma import --data export.<DATE>.zip it fails with an error for all relations in the postgresql database:

I have to run prisma deploy first to make sure the schema is updated but when it imports the data it is trying to import the relations twice which is violating some sort of duplicate constraint.

 "Failure inserting into relationtable _ExampleForOtherExample with ids StringIdGCValue(ckamscvpi0eo00702ep0c7log) and StringIdGCValue(cka2n025p03ri0766hnoyxf8s). Cause: insert or update on table \"_ExampleForOtherExample\" violates foreign key econstraint \"_ExampleForOtherExample_A_fkey\"\n  Detail: Key (A)=(cjvba054700dz07236tafuscj) is not present in table \"Example\".",
"Failure inserting into relationtable _ThingToOtherThing with ids StringIdGCValue(ckbgyi96h0kl1079500z24pwu) and StringIdGCValue(ckbkvgde606s50855g62uhqsb). Cause: duplicate key value violates unique constraint \"_ThingToOtherThing_AB_unique\"\n  Detail: Key (\"A\", \"B\")=(ck3kbmjgl036x0788furaqxkg, ck6uvgy7o09p40723rw34tna1) already exists.",

I'm not sure why it imports everything except for relations.

(prisma version 1.30.5)


Solution

  • tldr;

    prisma1 export creates duplicate relation table row data in their export data that are not present in the origin data. The error can be solved by removing those duplicates.

    More Details

    I found out that when I use the prisma1 export command, it creates duplicate relation table rows in its exported json files. My source database did not have these duplicates but they get introduced into the exported files.

    I created a simple node script that parsed all the JSON files and printed out all the duplicate relation row column A IDs.

    With this data I carefully removed all the duplicates prisma1 export command created in my data. Once I did this, this error went away and I was able to use prisma1 import without issue.