Dinesh Prajapati
Dinesh Prajapati

Reputation: 9520

Foreign key ON DELETE CASCADE is not working in Android 2.2

I have 2 tables:

db.execSQL("Create Table Location(LocationID INTEGER PRIMARY KEY AUTOINCREMENT, 
Latitude TEXT, Longitude TEXT," +
" CellID TEXT, MCC TEXT, MNC TEXT, LAC TEXT, SendTime TEXT DEFAULT
(datetime('now','localtime')), SignalStrength INTEGER, LocationType TEXT)");

db.execSQL("Create Table Error_Exception(LocationID INTEGER, ExceptionID INTEGER
PRIMARY KEY AUTOINCREMENT, ModuleID TEXT, ClassName TEXT, ControlName TEXT, " +
"MethodName TEXT, ErrorDescription TEXT, ErrorDate TEXT DEFAULT
(datetime('now','localtime')),
FOREIGN KEY(LocationID) REFERENCES Location(LocationID)
ON DELETE CASCADE);");

When I tried to delete the rows in Error_Exception it is not deleting the rows in Location table.

I have tried with db.execSQL("PRAGMA foreign_keys=ON;"); also but still not working.

Please help

Upvotes: 2

Views: 2747

Answers (1)

You've told your database to delete rows in Error_Exception when the referenced rows in Location are deleted. You seem to have things backward.

If you want to delete rows in Location when rows in Error_Exception are deleted, you need to drop the foreign key constraint in Error_Exception, and add a foreign key constraint in Location.

Upvotes: 5

Related Questions