I am developing an Android app in Kotlin and I have a table PERSONS
that contains people and another table FACES
that contains faces that I relate to people. The idea is that when you delete an entry of PERSONS
, all the faces associated with it will be erased. I create the tables in this way:
const val SQL_CREATE_PERSONS =
"CREATE TABLE " + "PERSONS" + "(" +
"ID" + " TEXT(100) PRIMARY KEY, " +
"NAME" + " TEXT(50)
"); "
const val SQL_CREATE_FACES =
"CREATE TABLE " + "FACES" + "(" +
"ID" + " TEXT(100) PRIMARY KEY, " +
"PERSON_ID " + "TEXT(100), " +
"FOREIGN KEY " + "(PERSON_ID)" + " REFERENCES " +
"PERSONS" + "(" +
"ID" + ") "+
"ON DELETE CASCADE " +
"); "
I have not forgotten to activate the Foreign Keys that are disabled by default, I do it in onCreate()
:
override fun onCreate(db: SQLiteDatabase?) {
db?.execSQL("PRAGMA foreign_keys=ON;")
db?.execSQL(SQL_CREATE_PERSONS)
db?.execSQL(SQL_CREATE_FACES)
}
But at the time of executing the application and deleting an entry of PERSONS
its faces are still in the table FACES