SQLite Foreign Key Constraint Failed (code 787)












7















I ran into the Foreign Key Constraint Failed (code 787) error when I tried to upgrade my database. The only change I did was try to add a 4th entry to my InsertStatus. I looked around and I read that using ON DELETE CASCADE should solve my problem so I tried placing it at all my FK references and tried again but still the same problem.



Logcat points to my onUpgrade and all the DROP TABLES in it ( i tried removing it one at a time to see which ones were bad and apparently all of them were ).



Am I using ON DELETE CASCADE wrong? Or is it something else in my code?



InsertStatus



void InsertStatus(SQLiteDatabase db) {
ContentValues cv = new ContentValues();
cv.put(colStatusID, 0);
cv.put(colStatClass, "Active");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 1);
cv.put(colStatClass, "Settled");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 2);
cv.put(colStatClass, "Terminated");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 3);
cv.put(colStatClass, "");
db.insert(statTable, colStatusID, cv);
}


DatabaseHelper



db.execSQL("CREATE TABLE " + termsTable + " (" + colTermsID + " INTEGER PRIMARY KEY , " + colTermsClass + " TEXT)");

db.execSQL("CREATE TABLE " + periodTable + " (" + colPeriodID + " INTEGER PRIMARY KEY , " + colPeriodClass + " TEXT)");

db.execSQL("CREATE TABLE " + statTable + " (" + colStatusID + " INTEGER PRIMARY KEY , " + colStatClass + " TEXT)");

db.execSQL("CREATE TABLE " + accountsTable + " (" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
colName + " TEXT, " +
colAmount + " Integer, " +
colPurpose + " TEXT, " +
colTerms + " INTEGER NOT NULL, " +
colPeriod +" INTEGER NOT NULL, " +
colBalance +" INTEGER, "+
colStatus + " INTEGER DEFAULT '1'," +
colDate + " TEXT, " +
colEditDate + " TEXT, " +
"FOREIGN KEY (" + colTerms + ") REFERENCES " + termsTable + " (" + colTermsID + ") ON DELETE CASCADE," +
"FOREIGN KEY (" + colPeriod + ") REFERENCES " + periodTable + " (" + colPeriodID + ") ON DELETE CASCADE," +
"FOREIGN KEY (" + colStatus + ") REFERENCES " + statTable + " (" + colStatusID + ") ON DELETE CASCADE);");

db.execSQL("CREATE TABLE " + payTable + " (" + colPayID + " INTEGER PRIMARY KEY , " +
colGroupID + " INTEGER NOT NULL, " +
colPayBal + " TEXT, " +
colInterest + " TEXT, " +
colPayDue + " TEXT, " +
colDateDue + " TEXT, " +
colPaid + " Integer, " +
"FOREIGN KEY (" + colGroupID + ") REFERENCES " + accountsTable + " (" + colID + ") ON DELETE CASCADE);");

db.execSQL("CREATE VIEW " + viewAccs +
" AS SELECT " + accountsTable + "." + colID + " AS _id," +
" " + accountsTable + "." + colName + "," +
" " + accountsTable + "." + colAmount + "," +
" " + accountsTable + "." + colPurpose + "," +
" " + termsTable + "." + colTermsClass + "," +
" " + periodTable + "." + colPeriodClass + "," +
" " + accountsTable+ "." + colBalance + "," +
" " + statTable + "." + colStatClass + "," +
" " + accountsTable + "." + colDate + "," +
" " + accountsTable + "." + colEditDate + "" +
" FROM " + accountsTable +
" JOIN " + termsTable + " ON " + accountsTable + "." + colTerms + " = " + termsTable + "." + colTermsID +
" JOIN " + periodTable + " ON " + accountsTable + "." + colPeriod + " = " + periodTable + "." + colPeriodID +
" JOIN " + statTable + " ON " + accountsTable + "." + colStatus + " = " + statTable + "." + colStatusID );

db.execSQL("CREATE VIEW " + viewPmnts +
" AS SELECT " + payTable + "." + colPayID + " AS _id," +
" " + accountsTable + "." + colID + "," +
" " + payTable + "." + colGroupID + "," +
" " + payTable + "." + colPayBal + "," +
" " + payTable + "." + colInterest + "," +
" " + payTable + "." + colPayDue + "," +
" " + payTable + "." + colDateDue + "," +
" " + payTable + "." + colPaid + "" +
" FROM " + payTable +
" JOIN " + accountsTable + " ON " + payTable + "." + colGroupID + " = " + accountsTable + "." + colID );

InsertTerms(db);
InsertPeriods(db);
InsertStatus(db);
}


onUpgrade



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


db.execSQL("DROP TABLE IF EXISTS " + accountsTable);
db.execSQL("DROP TABLE IF EXISTS " + termsTable);
db.execSQL("DROP TABLE IF EXISTS " + periodTable);
db.execSQL("DROP TABLE IF EXISTS " + statTable);
db.execSQL("DROP TABLE IF EXISTS " + payTable);

db.execSQL("DROP TRIGGER IF EXISTS acc_id_trigger");
db.execSQL("DROP TRIGGER IF EXISTS acc_id_trigger22");
db.execSQL("DROP TRIGGER IF EXISTS fk_accterm_termid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accperiod_periodid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accpay_payid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accstat_statid");

db.execSQL("DROP VIEW IF EXISTS " + viewAccs);
db.execSQL("DROP VIEW IF EXISTS " + viewPmnts);

onCreate(db);
}









share|improve this question

























  • did you try by uninstalling your application and try again after installing?

    – Keshav1234
    Mar 30 '15 at 9:06











  • Yeah, still the same result. Code 787. How though? This isn't the first time I've changed the contents of InsertStatus and I've never run into this before.

    – Cai
    Mar 30 '15 at 11:16











  • You can track the error easily if you use a debugger by adding a break point.

    – Keshav1234
    Mar 30 '15 at 11:17
















7















I ran into the Foreign Key Constraint Failed (code 787) error when I tried to upgrade my database. The only change I did was try to add a 4th entry to my InsertStatus. I looked around and I read that using ON DELETE CASCADE should solve my problem so I tried placing it at all my FK references and tried again but still the same problem.



Logcat points to my onUpgrade and all the DROP TABLES in it ( i tried removing it one at a time to see which ones were bad and apparently all of them were ).



Am I using ON DELETE CASCADE wrong? Or is it something else in my code?



InsertStatus



void InsertStatus(SQLiteDatabase db) {
ContentValues cv = new ContentValues();
cv.put(colStatusID, 0);
cv.put(colStatClass, "Active");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 1);
cv.put(colStatClass, "Settled");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 2);
cv.put(colStatClass, "Terminated");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 3);
cv.put(colStatClass, "");
db.insert(statTable, colStatusID, cv);
}


DatabaseHelper



db.execSQL("CREATE TABLE " + termsTable + " (" + colTermsID + " INTEGER PRIMARY KEY , " + colTermsClass + " TEXT)");

db.execSQL("CREATE TABLE " + periodTable + " (" + colPeriodID + " INTEGER PRIMARY KEY , " + colPeriodClass + " TEXT)");

db.execSQL("CREATE TABLE " + statTable + " (" + colStatusID + " INTEGER PRIMARY KEY , " + colStatClass + " TEXT)");

db.execSQL("CREATE TABLE " + accountsTable + " (" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
colName + " TEXT, " +
colAmount + " Integer, " +
colPurpose + " TEXT, " +
colTerms + " INTEGER NOT NULL, " +
colPeriod +" INTEGER NOT NULL, " +
colBalance +" INTEGER, "+
colStatus + " INTEGER DEFAULT '1'," +
colDate + " TEXT, " +
colEditDate + " TEXT, " +
"FOREIGN KEY (" + colTerms + ") REFERENCES " + termsTable + " (" + colTermsID + ") ON DELETE CASCADE," +
"FOREIGN KEY (" + colPeriod + ") REFERENCES " + periodTable + " (" + colPeriodID + ") ON DELETE CASCADE," +
"FOREIGN KEY (" + colStatus + ") REFERENCES " + statTable + " (" + colStatusID + ") ON DELETE CASCADE);");

db.execSQL("CREATE TABLE " + payTable + " (" + colPayID + " INTEGER PRIMARY KEY , " +
colGroupID + " INTEGER NOT NULL, " +
colPayBal + " TEXT, " +
colInterest + " TEXT, " +
colPayDue + " TEXT, " +
colDateDue + " TEXT, " +
colPaid + " Integer, " +
"FOREIGN KEY (" + colGroupID + ") REFERENCES " + accountsTable + " (" + colID + ") ON DELETE CASCADE);");

db.execSQL("CREATE VIEW " + viewAccs +
" AS SELECT " + accountsTable + "." + colID + " AS _id," +
" " + accountsTable + "." + colName + "," +
" " + accountsTable + "." + colAmount + "," +
" " + accountsTable + "." + colPurpose + "," +
" " + termsTable + "." + colTermsClass + "," +
" " + periodTable + "." + colPeriodClass + "," +
" " + accountsTable+ "." + colBalance + "," +
" " + statTable + "." + colStatClass + "," +
" " + accountsTable + "." + colDate + "," +
" " + accountsTable + "." + colEditDate + "" +
" FROM " + accountsTable +
" JOIN " + termsTable + " ON " + accountsTable + "." + colTerms + " = " + termsTable + "." + colTermsID +
" JOIN " + periodTable + " ON " + accountsTable + "." + colPeriod + " = " + periodTable + "." + colPeriodID +
" JOIN " + statTable + " ON " + accountsTable + "." + colStatus + " = " + statTable + "." + colStatusID );

db.execSQL("CREATE VIEW " + viewPmnts +
" AS SELECT " + payTable + "." + colPayID + " AS _id," +
" " + accountsTable + "." + colID + "," +
" " + payTable + "." + colGroupID + "," +
" " + payTable + "." + colPayBal + "," +
" " + payTable + "." + colInterest + "," +
" " + payTable + "." + colPayDue + "," +
" " + payTable + "." + colDateDue + "," +
" " + payTable + "." + colPaid + "" +
" FROM " + payTable +
" JOIN " + accountsTable + " ON " + payTable + "." + colGroupID + " = " + accountsTable + "." + colID );

InsertTerms(db);
InsertPeriods(db);
InsertStatus(db);
}


onUpgrade



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


db.execSQL("DROP TABLE IF EXISTS " + accountsTable);
db.execSQL("DROP TABLE IF EXISTS " + termsTable);
db.execSQL("DROP TABLE IF EXISTS " + periodTable);
db.execSQL("DROP TABLE IF EXISTS " + statTable);
db.execSQL("DROP TABLE IF EXISTS " + payTable);

db.execSQL("DROP TRIGGER IF EXISTS acc_id_trigger");
db.execSQL("DROP TRIGGER IF EXISTS acc_id_trigger22");
db.execSQL("DROP TRIGGER IF EXISTS fk_accterm_termid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accperiod_periodid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accpay_payid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accstat_statid");

db.execSQL("DROP VIEW IF EXISTS " + viewAccs);
db.execSQL("DROP VIEW IF EXISTS " + viewPmnts);

onCreate(db);
}









share|improve this question

























  • did you try by uninstalling your application and try again after installing?

    – Keshav1234
    Mar 30 '15 at 9:06











  • Yeah, still the same result. Code 787. How though? This isn't the first time I've changed the contents of InsertStatus and I've never run into this before.

    – Cai
    Mar 30 '15 at 11:16











  • You can track the error easily if you use a debugger by adding a break point.

    – Keshav1234
    Mar 30 '15 at 11:17














7












7








7


1






I ran into the Foreign Key Constraint Failed (code 787) error when I tried to upgrade my database. The only change I did was try to add a 4th entry to my InsertStatus. I looked around and I read that using ON DELETE CASCADE should solve my problem so I tried placing it at all my FK references and tried again but still the same problem.



Logcat points to my onUpgrade and all the DROP TABLES in it ( i tried removing it one at a time to see which ones were bad and apparently all of them were ).



Am I using ON DELETE CASCADE wrong? Or is it something else in my code?



InsertStatus



void InsertStatus(SQLiteDatabase db) {
ContentValues cv = new ContentValues();
cv.put(colStatusID, 0);
cv.put(colStatClass, "Active");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 1);
cv.put(colStatClass, "Settled");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 2);
cv.put(colStatClass, "Terminated");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 3);
cv.put(colStatClass, "");
db.insert(statTable, colStatusID, cv);
}


DatabaseHelper



db.execSQL("CREATE TABLE " + termsTable + " (" + colTermsID + " INTEGER PRIMARY KEY , " + colTermsClass + " TEXT)");

db.execSQL("CREATE TABLE " + periodTable + " (" + colPeriodID + " INTEGER PRIMARY KEY , " + colPeriodClass + " TEXT)");

db.execSQL("CREATE TABLE " + statTable + " (" + colStatusID + " INTEGER PRIMARY KEY , " + colStatClass + " TEXT)");

db.execSQL("CREATE TABLE " + accountsTable + " (" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
colName + " TEXT, " +
colAmount + " Integer, " +
colPurpose + " TEXT, " +
colTerms + " INTEGER NOT NULL, " +
colPeriod +" INTEGER NOT NULL, " +
colBalance +" INTEGER, "+
colStatus + " INTEGER DEFAULT '1'," +
colDate + " TEXT, " +
colEditDate + " TEXT, " +
"FOREIGN KEY (" + colTerms + ") REFERENCES " + termsTable + " (" + colTermsID + ") ON DELETE CASCADE," +
"FOREIGN KEY (" + colPeriod + ") REFERENCES " + periodTable + " (" + colPeriodID + ") ON DELETE CASCADE," +
"FOREIGN KEY (" + colStatus + ") REFERENCES " + statTable + " (" + colStatusID + ") ON DELETE CASCADE);");

db.execSQL("CREATE TABLE " + payTable + " (" + colPayID + " INTEGER PRIMARY KEY , " +
colGroupID + " INTEGER NOT NULL, " +
colPayBal + " TEXT, " +
colInterest + " TEXT, " +
colPayDue + " TEXT, " +
colDateDue + " TEXT, " +
colPaid + " Integer, " +
"FOREIGN KEY (" + colGroupID + ") REFERENCES " + accountsTable + " (" + colID + ") ON DELETE CASCADE);");

db.execSQL("CREATE VIEW " + viewAccs +
" AS SELECT " + accountsTable + "." + colID + " AS _id," +
" " + accountsTable + "." + colName + "," +
" " + accountsTable + "." + colAmount + "," +
" " + accountsTable + "." + colPurpose + "," +
" " + termsTable + "." + colTermsClass + "," +
" " + periodTable + "." + colPeriodClass + "," +
" " + accountsTable+ "." + colBalance + "," +
" " + statTable + "." + colStatClass + "," +
" " + accountsTable + "." + colDate + "," +
" " + accountsTable + "." + colEditDate + "" +
" FROM " + accountsTable +
" JOIN " + termsTable + " ON " + accountsTable + "." + colTerms + " = " + termsTable + "." + colTermsID +
" JOIN " + periodTable + " ON " + accountsTable + "." + colPeriod + " = " + periodTable + "." + colPeriodID +
" JOIN " + statTable + " ON " + accountsTable + "." + colStatus + " = " + statTable + "." + colStatusID );

db.execSQL("CREATE VIEW " + viewPmnts +
" AS SELECT " + payTable + "." + colPayID + " AS _id," +
" " + accountsTable + "." + colID + "," +
" " + payTable + "." + colGroupID + "," +
" " + payTable + "." + colPayBal + "," +
" " + payTable + "." + colInterest + "," +
" " + payTable + "." + colPayDue + "," +
" " + payTable + "." + colDateDue + "," +
" " + payTable + "." + colPaid + "" +
" FROM " + payTable +
" JOIN " + accountsTable + " ON " + payTable + "." + colGroupID + " = " + accountsTable + "." + colID );

InsertTerms(db);
InsertPeriods(db);
InsertStatus(db);
}


onUpgrade



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


db.execSQL("DROP TABLE IF EXISTS " + accountsTable);
db.execSQL("DROP TABLE IF EXISTS " + termsTable);
db.execSQL("DROP TABLE IF EXISTS " + periodTable);
db.execSQL("DROP TABLE IF EXISTS " + statTable);
db.execSQL("DROP TABLE IF EXISTS " + payTable);

db.execSQL("DROP TRIGGER IF EXISTS acc_id_trigger");
db.execSQL("DROP TRIGGER IF EXISTS acc_id_trigger22");
db.execSQL("DROP TRIGGER IF EXISTS fk_accterm_termid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accperiod_periodid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accpay_payid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accstat_statid");

db.execSQL("DROP VIEW IF EXISTS " + viewAccs);
db.execSQL("DROP VIEW IF EXISTS " + viewPmnts);

onCreate(db);
}









share|improve this question
















I ran into the Foreign Key Constraint Failed (code 787) error when I tried to upgrade my database. The only change I did was try to add a 4th entry to my InsertStatus. I looked around and I read that using ON DELETE CASCADE should solve my problem so I tried placing it at all my FK references and tried again but still the same problem.



Logcat points to my onUpgrade and all the DROP TABLES in it ( i tried removing it one at a time to see which ones were bad and apparently all of them were ).



Am I using ON DELETE CASCADE wrong? Or is it something else in my code?



InsertStatus



void InsertStatus(SQLiteDatabase db) {
ContentValues cv = new ContentValues();
cv.put(colStatusID, 0);
cv.put(colStatClass, "Active");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 1);
cv.put(colStatClass, "Settled");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 2);
cv.put(colStatClass, "Terminated");
db.insert(statTable, colStatusID, cv);
cv.put(colStatusID, 3);
cv.put(colStatClass, "");
db.insert(statTable, colStatusID, cv);
}


DatabaseHelper



db.execSQL("CREATE TABLE " + termsTable + " (" + colTermsID + " INTEGER PRIMARY KEY , " + colTermsClass + " TEXT)");

db.execSQL("CREATE TABLE " + periodTable + " (" + colPeriodID + " INTEGER PRIMARY KEY , " + colPeriodClass + " TEXT)");

db.execSQL("CREATE TABLE " + statTable + " (" + colStatusID + " INTEGER PRIMARY KEY , " + colStatClass + " TEXT)");

db.execSQL("CREATE TABLE " + accountsTable + " (" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
colName + " TEXT, " +
colAmount + " Integer, " +
colPurpose + " TEXT, " +
colTerms + " INTEGER NOT NULL, " +
colPeriod +" INTEGER NOT NULL, " +
colBalance +" INTEGER, "+
colStatus + " INTEGER DEFAULT '1'," +
colDate + " TEXT, " +
colEditDate + " TEXT, " +
"FOREIGN KEY (" + colTerms + ") REFERENCES " + termsTable + " (" + colTermsID + ") ON DELETE CASCADE," +
"FOREIGN KEY (" + colPeriod + ") REFERENCES " + periodTable + " (" + colPeriodID + ") ON DELETE CASCADE," +
"FOREIGN KEY (" + colStatus + ") REFERENCES " + statTable + " (" + colStatusID + ") ON DELETE CASCADE);");

db.execSQL("CREATE TABLE " + payTable + " (" + colPayID + " INTEGER PRIMARY KEY , " +
colGroupID + " INTEGER NOT NULL, " +
colPayBal + " TEXT, " +
colInterest + " TEXT, " +
colPayDue + " TEXT, " +
colDateDue + " TEXT, " +
colPaid + " Integer, " +
"FOREIGN KEY (" + colGroupID + ") REFERENCES " + accountsTable + " (" + colID + ") ON DELETE CASCADE);");

db.execSQL("CREATE VIEW " + viewAccs +
" AS SELECT " + accountsTable + "." + colID + " AS _id," +
" " + accountsTable + "." + colName + "," +
" " + accountsTable + "." + colAmount + "," +
" " + accountsTable + "." + colPurpose + "," +
" " + termsTable + "." + colTermsClass + "," +
" " + periodTable + "." + colPeriodClass + "," +
" " + accountsTable+ "." + colBalance + "," +
" " + statTable + "." + colStatClass + "," +
" " + accountsTable + "." + colDate + "," +
" " + accountsTable + "." + colEditDate + "" +
" FROM " + accountsTable +
" JOIN " + termsTable + " ON " + accountsTable + "." + colTerms + " = " + termsTable + "." + colTermsID +
" JOIN " + periodTable + " ON " + accountsTable + "." + colPeriod + " = " + periodTable + "." + colPeriodID +
" JOIN " + statTable + " ON " + accountsTable + "." + colStatus + " = " + statTable + "." + colStatusID );

db.execSQL("CREATE VIEW " + viewPmnts +
" AS SELECT " + payTable + "." + colPayID + " AS _id," +
" " + accountsTable + "." + colID + "," +
" " + payTable + "." + colGroupID + "," +
" " + payTable + "." + colPayBal + "," +
" " + payTable + "." + colInterest + "," +
" " + payTable + "." + colPayDue + "," +
" " + payTable + "." + colDateDue + "," +
" " + payTable + "." + colPaid + "" +
" FROM " + payTable +
" JOIN " + accountsTable + " ON " + payTable + "." + colGroupID + " = " + accountsTable + "." + colID );

InsertTerms(db);
InsertPeriods(db);
InsertStatus(db);
}


onUpgrade



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


db.execSQL("DROP TABLE IF EXISTS " + accountsTable);
db.execSQL("DROP TABLE IF EXISTS " + termsTable);
db.execSQL("DROP TABLE IF EXISTS " + periodTable);
db.execSQL("DROP TABLE IF EXISTS " + statTable);
db.execSQL("DROP TABLE IF EXISTS " + payTable);

db.execSQL("DROP TRIGGER IF EXISTS acc_id_trigger");
db.execSQL("DROP TRIGGER IF EXISTS acc_id_trigger22");
db.execSQL("DROP TRIGGER IF EXISTS fk_accterm_termid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accperiod_periodid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accpay_payid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accstat_statid");

db.execSQL("DROP VIEW IF EXISTS " + viewAccs);
db.execSQL("DROP VIEW IF EXISTS " + viewPmnts);

onCreate(db);
}






android sqlite foreign-keys foreign-key-relationship






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 1 '15 at 15:13







Cai

















asked Mar 30 '15 at 8:17









CaiCai

1,48121122




1,48121122













  • did you try by uninstalling your application and try again after installing?

    – Keshav1234
    Mar 30 '15 at 9:06











  • Yeah, still the same result. Code 787. How though? This isn't the first time I've changed the contents of InsertStatus and I've never run into this before.

    – Cai
    Mar 30 '15 at 11:16











  • You can track the error easily if you use a debugger by adding a break point.

    – Keshav1234
    Mar 30 '15 at 11:17



















  • did you try by uninstalling your application and try again after installing?

    – Keshav1234
    Mar 30 '15 at 9:06











  • Yeah, still the same result. Code 787. How though? This isn't the first time I've changed the contents of InsertStatus and I've never run into this before.

    – Cai
    Mar 30 '15 at 11:16











  • You can track the error easily if you use a debugger by adding a break point.

    – Keshav1234
    Mar 30 '15 at 11:17

















did you try by uninstalling your application and try again after installing?

– Keshav1234
Mar 30 '15 at 9:06





did you try by uninstalling your application and try again after installing?

– Keshav1234
Mar 30 '15 at 9:06













Yeah, still the same result. Code 787. How though? This isn't the first time I've changed the contents of InsertStatus and I've never run into this before.

– Cai
Mar 30 '15 at 11:16





Yeah, still the same result. Code 787. How though? This isn't the first time I've changed the contents of InsertStatus and I've never run into this before.

– Cai
Mar 30 '15 at 11:16













You can track the error easily if you use a debugger by adding a break point.

– Keshav1234
Mar 30 '15 at 11:17





You can track the error easily if you use a debugger by adding a break point.

– Keshav1234
Mar 30 '15 at 11:17












4 Answers
4






active

oldest

votes


















7














According to the below link you insert the value which failed the constraints of foreign key means you added a value of foregin key which not exists in parent table



https://www.sqlite.org/foreignkeys.html






share|improve this answer
























  • But everything is there, I pulled the file and loaded it into DBBrowser and it's all there. I don't see what's missing.

    – Cai
    Mar 30 '15 at 11:17











  • Can you send me that code?

    – cnnagpal
    Mar 30 '15 at 11:29











  • What do you mean code? The DB file?

    – Cai
    Mar 30 '15 at 12:56











  • send me code i will try to solve this problem

    – cnnagpal
    Mar 31 '15 at 3:59



















2














The error here is related to creating a child entity before their parent is in existence.



The Flow should be:



--Create Parent and get parent ID.
---- Create child entity containing a reference to parent ID.



So If you create a child entity without first having a valid parent ID you will be thrown this fatal error.






share|improve this answer































    1














    In my case I forgot to fill a parent table with data. When added an item to child table, got this error. When removed foreignKeys lines in Entity-file:



    foreignKeys = [
    ForeignKey(entity = SomeEntity::class, parentColumns = ["id"], childColumns = ["parent_id"]),
    ...
    ]


    insertion became possible. (But if you do that without clearing app data, you will get an exception java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.)



    So, simply add necessary data to parent tables.



    UPDATE



    I got this exception again.



    In order to test which foreign key is conficting, comment several of them and recompile the application. Also delete installed one or wipe it's data. After starting the app execute requests as usual. If no exception happened, uncomment one foreign key. Then delete the app again, recompile, add data to the table. Do it until you know which foreign key is violated.



    In my case I inserted 0 instead of null into a column, so that a parent table didn't contain 0.






    share|improve this answer

































      0














      Make sure that you first insert the object which is in the parent table. I was doing the same mistake while making some tests (not adding the corresponding parent, so that PK from the parent table was non existing.)



      Hope that fixes your problem.






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29341380%2fsqlite-foreign-key-constraint-failed-code-787%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        7














        According to the below link you insert the value which failed the constraints of foreign key means you added a value of foregin key which not exists in parent table



        https://www.sqlite.org/foreignkeys.html






        share|improve this answer
























        • But everything is there, I pulled the file and loaded it into DBBrowser and it's all there. I don't see what's missing.

          – Cai
          Mar 30 '15 at 11:17











        • Can you send me that code?

          – cnnagpal
          Mar 30 '15 at 11:29











        • What do you mean code? The DB file?

          – Cai
          Mar 30 '15 at 12:56











        • send me code i will try to solve this problem

          – cnnagpal
          Mar 31 '15 at 3:59
















        7














        According to the below link you insert the value which failed the constraints of foreign key means you added a value of foregin key which not exists in parent table



        https://www.sqlite.org/foreignkeys.html






        share|improve this answer
























        • But everything is there, I pulled the file and loaded it into DBBrowser and it's all there. I don't see what's missing.

          – Cai
          Mar 30 '15 at 11:17











        • Can you send me that code?

          – cnnagpal
          Mar 30 '15 at 11:29











        • What do you mean code? The DB file?

          – Cai
          Mar 30 '15 at 12:56











        • send me code i will try to solve this problem

          – cnnagpal
          Mar 31 '15 at 3:59














        7












        7








        7







        According to the below link you insert the value which failed the constraints of foreign key means you added a value of foregin key which not exists in parent table



        https://www.sqlite.org/foreignkeys.html






        share|improve this answer













        According to the below link you insert the value which failed the constraints of foreign key means you added a value of foregin key which not exists in parent table



        https://www.sqlite.org/foreignkeys.html







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 30 '15 at 9:09









        cnnagpalcnnagpal

        23718




        23718













        • But everything is there, I pulled the file and loaded it into DBBrowser and it's all there. I don't see what's missing.

          – Cai
          Mar 30 '15 at 11:17











        • Can you send me that code?

          – cnnagpal
          Mar 30 '15 at 11:29











        • What do you mean code? The DB file?

          – Cai
          Mar 30 '15 at 12:56











        • send me code i will try to solve this problem

          – cnnagpal
          Mar 31 '15 at 3:59



















        • But everything is there, I pulled the file and loaded it into DBBrowser and it's all there. I don't see what's missing.

          – Cai
          Mar 30 '15 at 11:17











        • Can you send me that code?

          – cnnagpal
          Mar 30 '15 at 11:29











        • What do you mean code? The DB file?

          – Cai
          Mar 30 '15 at 12:56











        • send me code i will try to solve this problem

          – cnnagpal
          Mar 31 '15 at 3:59

















        But everything is there, I pulled the file and loaded it into DBBrowser and it's all there. I don't see what's missing.

        – Cai
        Mar 30 '15 at 11:17





        But everything is there, I pulled the file and loaded it into DBBrowser and it's all there. I don't see what's missing.

        – Cai
        Mar 30 '15 at 11:17













        Can you send me that code?

        – cnnagpal
        Mar 30 '15 at 11:29





        Can you send me that code?

        – cnnagpal
        Mar 30 '15 at 11:29













        What do you mean code? The DB file?

        – Cai
        Mar 30 '15 at 12:56





        What do you mean code? The DB file?

        – Cai
        Mar 30 '15 at 12:56













        send me code i will try to solve this problem

        – cnnagpal
        Mar 31 '15 at 3:59





        send me code i will try to solve this problem

        – cnnagpal
        Mar 31 '15 at 3:59













        2














        The error here is related to creating a child entity before their parent is in existence.



        The Flow should be:



        --Create Parent and get parent ID.
        ---- Create child entity containing a reference to parent ID.



        So If you create a child entity without first having a valid parent ID you will be thrown this fatal error.






        share|improve this answer




























          2














          The error here is related to creating a child entity before their parent is in existence.



          The Flow should be:



          --Create Parent and get parent ID.
          ---- Create child entity containing a reference to parent ID.



          So If you create a child entity without first having a valid parent ID you will be thrown this fatal error.






          share|improve this answer


























            2












            2








            2







            The error here is related to creating a child entity before their parent is in existence.



            The Flow should be:



            --Create Parent and get parent ID.
            ---- Create child entity containing a reference to parent ID.



            So If you create a child entity without first having a valid parent ID you will be thrown this fatal error.






            share|improve this answer













            The error here is related to creating a child entity before their parent is in existence.



            The Flow should be:



            --Create Parent and get parent ID.
            ---- Create child entity containing a reference to parent ID.



            So If you create a child entity without first having a valid parent ID you will be thrown this fatal error.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 16 at 13:59









            paul_fpaul_f

            8617




            8617























                1














                In my case I forgot to fill a parent table with data. When added an item to child table, got this error. When removed foreignKeys lines in Entity-file:



                foreignKeys = [
                ForeignKey(entity = SomeEntity::class, parentColumns = ["id"], childColumns = ["parent_id"]),
                ...
                ]


                insertion became possible. (But if you do that without clearing app data, you will get an exception java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.)



                So, simply add necessary data to parent tables.



                UPDATE



                I got this exception again.



                In order to test which foreign key is conficting, comment several of them and recompile the application. Also delete installed one or wipe it's data. After starting the app execute requests as usual. If no exception happened, uncomment one foreign key. Then delete the app again, recompile, add data to the table. Do it until you know which foreign key is violated.



                In my case I inserted 0 instead of null into a column, so that a parent table didn't contain 0.






                share|improve this answer






























                  1














                  In my case I forgot to fill a parent table with data. When added an item to child table, got this error. When removed foreignKeys lines in Entity-file:



                  foreignKeys = [
                  ForeignKey(entity = SomeEntity::class, parentColumns = ["id"], childColumns = ["parent_id"]),
                  ...
                  ]


                  insertion became possible. (But if you do that without clearing app data, you will get an exception java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.)



                  So, simply add necessary data to parent tables.



                  UPDATE



                  I got this exception again.



                  In order to test which foreign key is conficting, comment several of them and recompile the application. Also delete installed one or wipe it's data. After starting the app execute requests as usual. If no exception happened, uncomment one foreign key. Then delete the app again, recompile, add data to the table. Do it until you know which foreign key is violated.



                  In my case I inserted 0 instead of null into a column, so that a parent table didn't contain 0.






                  share|improve this answer




























                    1












                    1








                    1







                    In my case I forgot to fill a parent table with data. When added an item to child table, got this error. When removed foreignKeys lines in Entity-file:



                    foreignKeys = [
                    ForeignKey(entity = SomeEntity::class, parentColumns = ["id"], childColumns = ["parent_id"]),
                    ...
                    ]


                    insertion became possible. (But if you do that without clearing app data, you will get an exception java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.)



                    So, simply add necessary data to parent tables.



                    UPDATE



                    I got this exception again.



                    In order to test which foreign key is conficting, comment several of them and recompile the application. Also delete installed one or wipe it's data. After starting the app execute requests as usual. If no exception happened, uncomment one foreign key. Then delete the app again, recompile, add data to the table. Do it until you know which foreign key is violated.



                    In my case I inserted 0 instead of null into a column, so that a parent table didn't contain 0.






                    share|improve this answer















                    In my case I forgot to fill a parent table with data. When added an item to child table, got this error. When removed foreignKeys lines in Entity-file:



                    foreignKeys = [
                    ForeignKey(entity = SomeEntity::class, parentColumns = ["id"], childColumns = ["parent_id"]),
                    ...
                    ]


                    insertion became possible. (But if you do that without clearing app data, you will get an exception java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.)



                    So, simply add necessary data to parent tables.



                    UPDATE



                    I got this exception again.



                    In order to test which foreign key is conficting, comment several of them and recompile the application. Also delete installed one or wipe it's data. After starting the app execute requests as usual. If no exception happened, uncomment one foreign key. Then delete the app again, recompile, add data to the table. Do it until you know which foreign key is violated.



                    In my case I inserted 0 instead of null into a column, so that a parent table didn't contain 0.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 31 '18 at 12:02

























                    answered Aug 22 '18 at 10:40









                    CoolMindCoolMind

                    3,97213472




                    3,97213472























                        0














                        Make sure that you first insert the object which is in the parent table. I was doing the same mistake while making some tests (not adding the corresponding parent, so that PK from the parent table was non existing.)



                        Hope that fixes your problem.






                        share|improve this answer




























                          0














                          Make sure that you first insert the object which is in the parent table. I was doing the same mistake while making some tests (not adding the corresponding parent, so that PK from the parent table was non existing.)



                          Hope that fixes your problem.






                          share|improve this answer


























                            0












                            0








                            0







                            Make sure that you first insert the object which is in the parent table. I was doing the same mistake while making some tests (not adding the corresponding parent, so that PK from the parent table was non existing.)



                            Hope that fixes your problem.






                            share|improve this answer













                            Make sure that you first insert the object which is in the parent table. I was doing the same mistake while making some tests (not adding the corresponding parent, so that PK from the parent table was non existing.)



                            Hope that fixes your problem.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 15:56









                            Gabriel TrifaGabriel Trifa

                            114




                            114






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29341380%2fsqlite-foreign-key-constraint-failed-code-787%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith