6

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.

Ashique bzq
  • 541
  • 2
  • 9
  • 21

2 Answers2

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
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