I have the following database in Android:
Table A: [_id, value1]
Table B: [_id, fk_tablea, value2]
With code:
db.execSQL("CREATE TABLE IF NOT EXISTS table_A ( "
+ "_id long primary key , value1 long );");
db.execSQL("CREATE TABLE IF NOT EXISTS table_B ( "
+ "_id long primary key ,fk_tablea long , value2 long,"
+ "FOREIGN KEY (fk_tablea) REFERENCES table_A (value1) ON DELETE CASCADE);");
And if I make (with the table empty):
ContentValues values= new ContentValues();
values.put("_id",1);
values.put("value1",900);
mDb.replace("table_A", null, values);
or
mDb.execSQL("INSERT OR REPLACE INTO table_A(_id,value1) VALUES (1,900)");
of
mDb.insertWithOnConflict("table_A", null, values, SQLiteDatabase.CONFLICT_REPLACE);
It crashes with an exception Caused by: android.database.sqlite.SQLiteException: foreign key mismatch: , while compiling: INSERT OR REPLACE INTO table_A(_id,value1) VALUES (2,900) . But if instead of replace I use insert
ContentValues values= new ContentValues();
values.put("_id",1);
values.put("value1",900);
mDb.insert("table_A", null, values);
Works correctly, why is that happening? and how can I solve it? Thanks in advance