我想直接更新相关行。我该如何做到这一点,而无需在 Drift 中选择项目并一一更新它们? 我当前的代码:
final concernedSongs = await (select(playlistSongs)
..where((tbl) => tbl.order.isBetweenValues(low + 1, high)))
.get(); // should just update and decrement the values without reiterating over them
concernedSongs.forEach((song) {
reOrder(song.id, song.order - 1);
});
您可以使用自定义查询,参考文档
这个查询可能会帮助您了解一些想法,
await db.customStatement("update 'songs'
set order = order-1 where order between ? and ?",
[low+1,high]);
目前仅使用 dart 代码这是不可能的。 您应该考虑使用 customStatement。比如:
await db.customStatement(
"UPDATE 'songs' SET order = order - 1 WHERE order > ? AND order < ?",
[low + 1, high],
);
抱歉回复晚了,但我遇到了你的问题,因为我也遇到了同样的问题。我查看了 Drift Github 存储库,发现可以按如下方式进行:
await (_db.update(_db.items)
..where((e) => e.sequence.isBiggerOrEqualValue(insertAtSequence)))
.write(ItemsCompanion.custom(sequence: _db.items.sequence + const Constant(1))));
类似示例的来源以及包所有者的答案:https://github.com/simolus3/drift/issues/673#issuecomment-652617026