SQLite with foreign keys NULLABLE

I am having problems with NULLABLE foreign keys that result in a SQLiteConstraintException. Foreign keys are activated using:

PRAGMA foreign_keys = ON

Tables are created with the following statements:

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);

Operators are suppressed to improve reading.

As you can see, the foreign key "chat_id" is declared as NULLABLE, so I am not forced to bind each message to a conversation (for example, when a draft message is created that does not have a corresponding conversation).

When I try to insert a new message using dialog_id, everything will be fine. When I try without getting the following error:

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)

It seems that sqlite3 ignores my NULL constraint and forces every message to have a foreign key. What can I do to get column_index_id to NULL?

+4
1

NULL .

+3

All Articles