salehyarahmadi / RoomDatabaseBackupAndRestore

Android library for backup and restore room database
54 stars 6 forks source link

FOREIGN KEY constraint failed (code 787) #1

Open MustafaBasim opened 4 years ago

MustafaBasim commented 4 years ago

I have 4 tables with relations between them, so when I restored the database I got this error:

android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY[787])

but what was weird I didn't encounter it before when I first tested the library with all tables filled.

I realized the issue was when the backup file created, the library deletes the AUTOCOMPLETE ids hence the relations got broken.

The solution:

I edited the Backup class "Backup.java" file and removed the following code to fix the issue, line 83:

String tableSql = tableSqlCursor.getString(0);
tableSql = tableSql.substring(tableSql.indexOf("("));
String aic = "";
if(tableSql.contains("AUTOINCREMENT")){
    tableSql = tableSql.substring(0,tableSql.indexOf("AUTOINC
    tableSql = tableSql.substring(0,tableSql.lastIndexOf("`")
    aic = tableSql.substring(tableSql.lastIndexOf("`") + 1);
    }

also line 97:

if(columnName.equals(aic)) continue;

and it worked fine.

Haruhiism commented 3 years ago

I have 4 tables with relations between them, so when I restore the database I got this error:

android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY[787])

but what was weird I didn't encounter it before when I first tested the library with all tables filled.

I realized the issue was when the backup file has created the library deletes the AUTOCOMPLETE ids hence the relations get broken.

I edited the Backup class and removed the following code to fix the issue:

String tableSql = tableSqlCursor.getString(0);
tableSql = tableSql.substring(tableSql.indexOf("("));
String aic = "";
if(tableSql.contains("AUTOINCREMENT")){
    tableSql = tableSql.substring(0,tableSql.indexOf("AUTOINC
    tableSql = tableSql.substring(0,tableSql.lastIndexOf("`")
    aic = tableSql.substring(tableSql.lastIndexOf("`") + 1);

also:

if(columnName.equals(aic)) continue;

and it worked fine.

I am having the exact same issue and your solution didn't work for me. I also have a relational database that's quite empty at the moment except for a few relational entries to test this out. Any suggestions?

maclewan commented 3 years ago

The same problem here...

maclewan commented 3 years ago

I have 4 tables with relations between them, so when I restored the database I got this error:

android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY[787])

but what was weird I didn't encounter it before when I first tested the library with all tables filled.

I realized the issue was when the backup file created, the library deletes the AUTOCOMPLETE ids hence the relations got broken.

I edited the Backup class and removed the following code to fix the issue:

String tableSql = tableSqlCursor.getString(0);
tableSql = tableSql.substring(tableSql.indexOf("("));
String aic = "";
if(tableSql.contains("AUTOINCREMENT")){
    tableSql = tableSql.substring(0,tableSql.indexOf("AUTOINC
    tableSql = tableSql.substring(0,tableSql.lastIndexOf("`")
    aic = tableSql.substring(tableSql.lastIndexOf("`") + 1);

also:

if(columnName.equals(aic)) continue;

and it worked fine.

Looks like this solution works for me also! Thx!

dhiashalabi commented 3 years ago

The same problem here .

dhiashalabi commented 3 years ago

I have 4 tables with relations between them, so when I restored the database I got this error:

android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY[787])

but what was weird I didn't encounter it before when I first tested the library with all tables filled.

I realized the issue was when the backup file created, the library deletes the AUTOCOMPLETE ids hence the relations got broken.

The solution:

I edited the Backup class "Backup.java" file and removed the following code to fix the issue, line 83:

String tableSql = tableSqlCursor.getString(0);
tableSql = tableSql.substring(tableSql.indexOf("("));
String aic = "";
if(tableSql.contains("AUTOINCREMENT")){
    tableSql = tableSql.substring(0,tableSql.indexOf("AUTOINC
    tableSql = tableSql.substring(0,tableSql.lastIndexOf("`")
    aic = tableSql.substring(tableSql.lastIndexOf("`") + 1);
    }

also line 97:

if(columnName.equals(aic)) continue;

and it worked fine.

I am having the exact same problem and your solution didn't work for me. When i create a backup and reset my app data the restore process just works fine, but if the database in not empty the restore process did't work and it gives me the error FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY[787])

Thibaut-Esteve commented 1 year ago

Hi @MustafaBasim, It seems that I encounter the same issue, but I don't understand what is this file Backup.java? Where can I find it in my project please? I use Android Studio with kotlin and Jetpack Compose, maybe this file doesn't exist anymore on this version of Android Studio.

MustafaBasim commented 1 year ago

Hi @MustafaBasim, It seems that I encounter the same issue, but I don't understand what is this file Backup.java? Where can I find it in my project please? I use Android Studio with kotlin and Jetpack Compose, maybe this file doesn't exist anymore on this version of Android Studio.

Hi @Thibaut-Esteve , The file is in the same library, I loaded the library files to my project and edited them. This is the file: Backup.java