首页 > 解决方案 > 未处理的异常:SqliteException(1):没有这样的表:main.checkLists,SQL 逻辑错误(代码 1)[在 Moor]

问题描述

我发现大多数人只是通过在 Moor 中处理迁移来清理和重建他们的颤振数据库来解决这个问题。但是,我也做了同样的事情,但无济于事。

当前错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SqliteException(1): no such table: main.checkLists, SQL logic error (code 1)

当我将新任务插入任务时会发生这种情况

我的数据库中目前有两个表。

  1. 清单
  2. 任务

稍后添加了任务,我创建了一个MigrationStrategy如下:

MigrationStrategy get migration => MigrationStrategy(
    onCreate: (m) async {
      await m.createAll(); // create all tables
      final TasksCompanion task = TasksCompanion.insert(checkListId: 1, toDo:'First'); 
      await into(tasks).insert(task); // insert on first run.
    },

    onUpgrade: (migrator, from, to) async {
      if(from == 1){
        await migrator.createTable(tasks);
      }
    },
    beforeOpen: (details) async {
      await customStatement('PRAGMA foreign_keys = ON');
    },
  );

首次运行插入成功。含义 Tasks 表已创建。我不知道还能做什么,因为没有其他编译器错误。

请注意,错误声明 没有这样的表:main.checkLists但在将任务插入任务表时发生错误

这是我使用的插入算法

在 moorDatabase.dart 中

// inside Task Dao    
Future insertTask(Insertable<Task> task){
    return into(tasks).insert(task);
}

在taskScreen.dart

addTask(BuildContext context, String toDo, DateTime createdAt){
    final database = Provider.of<AppDatabase>(context, listen: false);
    final tasksDao = database.tasksDao;
    final TasksCompanion task = TasksCompanion.insert(checkListId: widget.checkListId, toDo: toDo, createdAt: Value.ofNullable(createdAt));
    tasksDao.insertTask(task);
}

标签: fluttersqlitemoor

解决方案


好的,所以我想通了。只需将此行添加到我的迁移策略中的onUpgrade方法中,因为checkListId列是稍后添加的。

await migrator.addColumn(tasks, tasks.checkListId);

在清理正在运行的 build_runner 之前,我还修改了我的架构版本


推荐阅读