title: SQLite with NULLABLE foreign keys
body: <p>I am having problems with NULLABLE foreign keys leading to an SQLiteConstraintException. Foreign keys are enabled by using:</p>
  
  <pre><code>PRAGMA foreign_keys = ON
  </code></pre>
  
  <p>The tables are created with the following statements:</p>
  
  <pre><code>CREATE TABLE conversation (_id INTEGER PRIMARY KEY);
  CREATE TABLE message (_id INTEGER PRIMARY KEY, conversation_id INTEGER, FOREIGN KEY(conversation_id) REFERENCES conversation (_id) ON DELETE CASCADE);
  </code></pre>
  
  <p>The statements are dumbed down to improve reading.</p>
  
  <p>As you can see, the foreign key 'conversation_id' is declared as NULLABLE, so that I am not forced to link every message to a conversation (for example when a draft message gets created that has no corresponding conversation).</p>
  
  <p>When I try to insert a new message with a conversation_id, everything goes fine. When I try it without I get the following error:</p>
  
  <pre><code>11-07 16:44:55.190  20582-20582/com.miawe.miabe E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: com.miawe.miabe, PID: 20582
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.miawe.miabe/com.miawe.miabe.gui.MainActivity}: android.database.sqlite.SQLiteConstraintException: foreign key constraint failed (code 19)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
          at android.app.ActivityThread.access$800(ActivityThread.java:135)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:136)
          at android.app.ActivityThread.main(ActivityThread.java:5017)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:515)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
          at dalvik.system.NativeStart.main(Native Method)
   Caused by: android.database.sqlite.SQLiteConstraintException: foreign key constraint failed (code 19)
          at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
          at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
          at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
          at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
          at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
          at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1365)
          at com.miawe.miabe.database.DatabaseDataSource.insert(DatabaseDataSource.java:244)
          at com.miawe.miabe.database.DatabaseDataSource.createDummyData(DatabaseDataSource.java:307)
          at com.miawe.miabe.gui.MainActivity.onCreate(MainActivity.java:64)
          at android.app.Activity.performCreate(Activity.java:5231)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
              at android.app.ActivityThread.access$800(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5017)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>It seems like sqlite3 is ignoring my NULL-constraint and forcing every message to have a foreign key. What can I do to make the column conversation_id accept NULL values?</p>