flutter - 未处理的异常: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)
当我将新任务插入任务时会发生这种情况
我的数据库中目前有两个表。
- 清单
- 任务
稍后添加了任务,我创建了一个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);
}
解决方案
好的,所以我想通了。只需将此行添加到我的迁移策略中的onUpgrade方法中,因为checkListId列是稍后添加的。
await migrator.addColumn(tasks, tasks.checkListId);
在清理正在运行的 build_runner 之前,我还修改了我的架构版本
推荐阅读
- c# - Avro.AvroException:数组未在字段中实现非泛型 IList
- python - 从一个文件中读取字典并使用python从熊猫创建一个excel(数据框)l
- mysql - mysql问题看两个select是否一样
- r - 如果最后一行要删除一列,R 函数不会返回 data.table
- azure-active-directory - 我们可以使用基于 nodejs 协议的库连接到 Azure AD
- android - 在 ViewModel 中使用刀柄注入时无法在可组合函数中使用 ViewModel
- reactjs - 将状态附加到动态呈现的元素
- php - Symfony 安全 IS_AUTHENTICATED_FULLY 无法正常工作
- windows - 如何编译mingw64中有qt6的代码?
- regex - 正则表达式匹配域但不匹配子域