未捕获到外键约束RESTRICT

时间:2019-06-22 15:13:25

标签: android android-room

我正在尝试使用Room在Android中捕获外键约束异常。

我无法在try-catch语句中使用特定的异常SQLiteConstraintException(最常见的Exception)捕获此运行时异常。

这是来自switch语句中菜单的try-catch:

try {
                    mViewModel.deleteTerm();
                } catch (SQLiteConstraintException ex) {
                    AlertDialog.Builder a_builder = new
                            AlertDialog.Builder(TermEditorActivity.this);
                    a_builder.setMessage("Courses are assigned for this term and it cannot be deleted." +
                            "\n\nYou must remove all courses prior to deleting a term.")
                            .setCancelable(false)
                            .setPositiveButton("Okay", new
                                    DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                            int which) {
                                            dialog.cancel();
                                            //finish();
                                        }
                                    });
                    AlertDialog deleteAllAlert = a_builder.create();
                    deleteAllAlert.setTitle("CANNOT DELETE TERM!!!");
                    deleteAllAlert.show();
                    return true;

                }
                finally {
                    startActivity(new Intent(TermEditorActivity.this,
                            MainActivity.class));
                }
            return true;

“运行”窗口中出现异常:

E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
    Process: com.mattspriggs.termtest, PID: 23927
    android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 1811 SQLITE_CONSTRAINT_TRIGGER)
        at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
        at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:784)
        at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
        at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
        at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeUpdateDelete(FrameworkSQLiteStatement.java:45)
        at android.arch.persistence.room.EntityDeletionOrUpdateAdapter.handle(EntityDeletionOrUpdateAdapter.java:70)
        at com.mattspriggs.termtest.database.TermDao_Impl.deleteTerm(TermDao_Impl.java:144)
        at com.mattspriggs.termtest.database.AppRepository$4.run(AppRepository.java:83)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)

这是RESTRICT文档。我所关心的是它如何声明异常的工作方式,这显然与其他异常不同:

"The difference between the effect of a RESTRICT action and normal foreign key constraint enforcement is that the RESTRICT action processing happens as soon as the field is updated - not at the end of the current statement as it would with an immediate constraint, or at the end of the current transaction as it would with a deferred() constraint.

Even if the foreign key constraint it is attached to is deferred(), configuring a RESTRICT action causes SQLite to return an error immediately if a parent key with dependent child keys is deleted or modified.

因此,如果这是一个SQLite错误,它真的属于Error吗?我没有得到如何捕获此异常的信息,并认为也许实际上这是异常服装中的一个错误?

我认为使用NO_ACTION可能会更易于处理,但这也没有被捕获,并且在日志中有相同的错误消息。

0 个答案:

没有答案