title: delete on cascade, error FOREIGN KEY constraint failed, sqlite
body: <p>When I delete a Chat row I want to delete all messages rows and all relations rows created with that <code>id_chat</code>. I don't have problems adding registers at this point but deleting relations and messages registers as they don't disappear automatically.</p>
  
  <pre><code>public class DatabaseHelper extends SQLiteOpenHelper {
      @Override
      public void onCreate(SQLiteDatabase db) {
           db.execSQL("CREATE TABLE Chats (_id INTEGER PRIMARY KEY AUTOINCREMENT, A1 DATE)");
           db.execSQL("CREATE TABLE Messages (_id INTEGER PRIMARY KEY AUTOINCREMENT, FOREIGN KEY(id_user) REFERENCES Users(id_user), FOREIGN KEY(id_chat) REFERENCES Chats(_id) ON DELETE CASCADE)");
           db.execSQL("CREATE TABLE Relations (id_viaje INTEGER, id_chat INTEGER, PRIMARY KEY (id_chat, id_viaje), FOREIGN KEY (id_chat) REFERENCES Chats(_id) ON DELETE CASCADE, FOREIGN KEY (id_viaje) REFERENCES Viajes(_id))");
           db.execSQL("CREATE TABLE Users (_id INTEGER AUTO_INCREMENT, id_user TEXT PRIMARY KEY, name TEXT)");
           db.execSQL("CREATE TABLE Viajes (_id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, activate BOOLEAN, id_user TEXT not null)");
       }
  }
  </code></pre>
  
  <p>So as I read in stackoverflow I overrode the method <code>onOpen</code> in order to activate the foreign keys:</p>
  
  <pre><code>public void onOpen(SQLiteDatabase db){
      super.onOpen(db);
      db.execSQL("PRAGMA foreign_keys = ON");
  }
  </code></pre>
  
  <p>edit:
  And now the first register in Relations was added (1,17) the second one (3,17) gives me an error in the local db:</p>
  
  <pre><code>Error inserting id_viaje=3 id_chat=17 android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed
  </code></pre>
  
  <p>Why this error? how to delete on cascade?
  Thanks</p>