I want to update only specified columns, when I execute update(table).replace(model)
it replaces all data corresponding to primary key. How to update only specified column without writing custom queries.
Asked
Active
Viewed 2,991 times
6

Ashique bzq
- 541
- 2
- 9
- 21
-
you can update based on their id. – John Joe Dec 30 '19 at 09:47
2 Answers
4
You have to declare your function with Insertable
like that:
Future updateVisit(Insertable<Visit> visit) => update(visits).replace(visit);
so, when you call a function you can do this:
visitDao.updateVisit(visit.copyWith(completed: newValue))
or
visitDao.updateVisit(VisitsCompanion(id: Value(visitId), checkOut: Value(DateTime.now())));

Boken
- 4,825
- 10
- 32
- 42

Rafael Menicucci
- 137
- 2
- 8
2
In the docs, I found this that might be helpful, depending on your use case. This is how you could 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),
),
);
}

valeriana
- 161
- 1
- 16