GithubHelp home page GithubHelp logo

Comments (5)

simolus3 avatar simolus3 commented on June 12, 2024 2

Be careful with that, I think it would update all tasks and apply the finished: false column to every row. If the task has a non-null id then the copyWith/replace call should work.
You could also do (update(tasks)..whereSamePrimaryKey(task)).write(TasksCompanion(finished: state)) - but again, only if task.id is not null.

from drift.

simolus3 avatar simolus3 commented on June 12, 2024 1

The only null value that I see during debug is the id(because we do copyWith)

So to be sure, you're calling replace with a Task object that has its id field set to null? That would explain the error, but I don't think drift can do much against that (except giving a better error message). Tasks are identified by their id and we need to know which task to replace, but we can't do that if id is null.

from drift.

simolus3 avatar simolus3 commented on June 12, 2024

Thanks for the report! Do you have an empty primary key? Could you post the definition of the Tasks table as well?

from drift.

kirqe avatar kirqe commented on June 12, 2024

I slightly updated the table names but the structure is basically the same
During task.copyWith the original task has the id

import 'package:json_annotation/json_annotation.dart' as ja;

@UseRowClass(Task)
class Tasks extends Table {
  IntColumn get id => integer().autoIncrement()();

  IntColumn get orderId => integer().references(Orders, #id, onDelete: KeyAction.cascade)();

  BoolColumn get finished => boolean().withDefault(const Constant(false))();
}

@ja.JsonSerializable()
class Task implements Insertable<Task> {
...

The "tasks" are fetched from api and saved like this

final Set<TasksCompanion> tasksCompanions;

tasks.forEach((taskJson) {
        tasksCompanions.add(Task.fromJson({
          'order_id': orderJson['id'],
          'finished': false
        }).toCompanion(true));
      });

... call daos

I checked the state. The items are being saved with no issues

from drift.

kirqe avatar kirqe commented on June 12, 2024

You're right. This works. Thank you!

 Future<void> toggleFinish(Task task, bool state) {
    return update(tasks)
        .write(TasksCompanion(finished: state));
  }

edit: oh, I meant where task, passing the existing task -___-

Another option is to search through other fields. update.where.write

from drift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.