颤动的停泊-仅更新指定的列而无需自定义查询

时间:2019-12-25 07:51:20

标签: android flutter dart dart-pub flutter-moor

我只想更新指定的列,当我执行update(table).replace(model)时,它将替换与主键相对应的所有数据。如何在不编写自定义查询的情况下仅更新指定的列。

2 个答案:

答案 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())));