我只想更新指定的列,当我执行update(table).replace(model)
时,它将替换与主键相对应的所有数据。如何在不编写自定义查询的情况下仅更新指定的列。
答案 0 :(得分:2)
在文档中,我发现这可能会有所帮助,具体取决于您的用例。这样你就可以replace values of a single column.
Future moveImportantTasksIntoCategory(Category target) {
// for updates, we use the "companion" version of a
generated class. This wraps the
// fields in a "Value" type which can be set to be absent using "Value.absent()". This
// allows us to separate between "SET category = NULL"
(`category: Value(null)`) and not
// updating the category at all: `category: Value.absent()`.
return (update(todos)
..where((t) => t.title.like('%Important%'))
).write(TodosCompanion(
category: Value(target.id),
),
);
}
答案 1 :(得分:1)
您必须像这样用Insertable
声明函数:
Future updateVisit(Insertable<Visit> visit) => update(visits).replace(visit);
因此,当您调用函数时,您可以执行以下操作:
visitDao.updateVisit(visit.copyWith(completed: newValue))
或
visitDao.updateVisit(VisitsCompanion(id: Value(visitId), checkOut: Value(DateTime.now())));