2

I have 2 tables:

Table 1- create table table1(_id integer primary key, content text not null );

Table 2- create table table2(_id integer primary key, id_parent integer, content text not null , CONSTRAINT [FK_table2_id_parent] FOREIGN KEY (id_parent) REFERENCES table1 (_id) ON DELETE CASCADE );

But the DELETE CASCADE doesn't do anything... Anyone know what I'm doing wrong?

[EDIT:

When I try to delete rows from table1 I can, but when I look for table2 the records are all there ... even those who have the id_parent that no longer exists from Table1.]

I'm using android 2.2.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    Please elaborate **"But the DELETE CASCADE dont do anything"**! Have you deleted anything from `table1`, and the rows with it's id reference remained in `table2`? – rekaszeru May 17 '12 at 11:26

1 Answers1

4

First make sure your used create table commands are syntactically and logically right.

If yes then this is due to following:

Declaring and defining foreign key does not apply them really to act. You need to explicitly on the foreign key constraint in your database.

if (!db.isReadOnly()) {
            // Enable foreign key constraints
            db.execSQL("PRAGMA foreign_keys=ON;");
        }
Arpit Garg
  • 8,476
  • 6
  • 34
  • 59