The argument type 'GeneratedIntColumn' can't be assigned to the parameter type 'int': Moor-Flutter

1,592

I have realized my mistake by looking at the code example from https://resocoder.com/2019/07/17/moor-room-for-flutter-3-foreign-keys-joins-migrations-fluent-sqlite-database/

Instead of tasks.id.equals(categories.taskId)

The expression in the join should be tasks.id.equalsExp(categories.taskId)

Share:
1,592
Barnett Temwa Msiska
Author by

Barnett Temwa Msiska

I am a creative and versatile App and Web Developer. Working primarily with PHP, MySQL, Javascript, HTML5, CSS3, Dart and Flutter. Also works extensively with Laravel, and VueJS Frameworks. Proponent of the Progressive Web App methodology (PWA). Develops for both Android and iOS with Flutter. Works with REST APIs. Passionate about Information Technology and how it is able to productively transform society and a strong believer in its potential to affect change. Lover and seeker of all true knowledge.

Updated on November 28, 2022

Comments

  • Barnett Temwa Msiska
    Barnett Temwa Msiska 4 days

    I am trying to set a foreign key relationship with Moor-Flutter when joining two tables and I get the error below in VS Code;

    The argument type 'GeneratedIntColumn' can't be assigned to the parameter type 'int'.

    At first I thought it is because I am using an auto increment field as a foreign key but now I am not so sure because I still get the error when I change the code.

    Here is my code. The error happens on the join statement when comparing; tasks.id.equals(categories.taskId).

    import 'package:moor/moor.dart';
    import 'package:moor_flutter/moor_flutter.dart';
    part 'moor_database.g.dart';
    @DataClassName("Category")
    class Categories extends Table {
      IntColumn get id => integer().autoIncrement()();
      IntColumn get taskId =>
          integer().nullable().customConstraint('NULL REFERENCES tasks(id)')();
      TextColumn get name => text().withLength(min: 1, max: 100)();
      TextColumn get icon => text()();
      TextColumn get color => text()();
    }
    class Tasks extends Table {
      IntColumn get id => integer().autoIncrement()();
      TextColumn get name => text().withLength(min: 1, max: 200)();
      BoolColumn get completed => boolean().withDefault(Constant(false))();
    }
    @UseMoor(tables: [Categories, Tasks], daos: [CategoryDao])
    class AppDatabase extends _$AppDatabase {
      AppDatabase()
          : super((FlutterQueryExecutor.inDatabaseFolder(
              path: 'todo.sqlite',
              logStatements: true,
            )));
      @override
      int get schemaVersion => 1;
      @override
      MigrationStrategy get migration =>
          MigrationStrategy(beforeOpen: (details) async {
            await customStatement('PRAGMA foreign_keys = ON');
          });
    }
    class CategoryWithTask {
      final Category category;
      final Task task;
      CategoryWithTask({@required this.category, @required this.task});
    }
    @UseDao(tables: [Categories, Tasks])
    class CategoryDao extends DatabaseAccessor<AppDatabase>
        with _$CategoryDaoMixin {
      final AppDatabase db;
      CategoryDao(this.db) : super(db);
      Stream<List<CategoryWithTask>> watchAllCategories() {
        return (select(categories)
              ..orderBy(([
                (c) => OrderingTerm(expression: c.name),
              ])))
            .join([leftOuterJoin(tasks, tasks.id.equals(categories.taskId))])
            .watch()
            .map((rows) => rows.map((row) {
                  return CategoryWithTask(
                      category: row.readTable(categories),
                      task: row.readTable(tasks));
                }));
      }
    }