simolus3 / drift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.
https://drift.simonbinder.eu/
MIT License
2.42k stars 349 forks source link

Build error inheriting public base table #2953

Closed disonwei closed 1 month ago

disonwei commented 1 month ago
abstract class BaseTable extends Table {
  IntColumn get creatorId => integer().nullable().references(UsersTable, #id)();

  IntColumn get updaterId => integer().nullable().references(UsersTable, #id)();

  IntColumn get deleterId => integer().nullable().references(UsersTable, #id)();

  DateTimeColumn get createAt => dateTime()();

  DateTimeColumn get updateAt => dateTime()();

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

@DataClassName('Category')
class CategoryTable extends  BaseTable  {
  @override
  String get tableName => 'category';

  IntColumn get id => integer().autoIncrement()();

  IntColumn get parentId =>
      integer().nullable().customConstraint('NULL REFERENCES category(id)')();
}

@DataClassName('Billing')
class BillingTable extends  BaseTable {
  @override
  String get tableName => 'billing';

  IntColumn get id => integer().autoIncrement()();

  IntColumn get categoryId =>
      integer().nullable().references(CategoryTable, #id)();

  IntColumn get ledgerId => integer().nullable().references(LedgerTable, #id)();

  IntColumn get labelId => integer().nullable().references(LabelTable, #id)();

  IntColumn get assetsId => integer().nullable().references(AssetsTable, #id)();
}
[WARNING] drift_dev on lib/app/db/tables/billing.dart:
line 12, column 62 of package:min_budget_flutter/app/db/tables/common/BaseTable.dart: The referenced element, UsersTable, is not understood by drift.
   ╷
12 │   IntColumn get deleterId => integer().nullable().references(UsersTable, #id)();
   │                                                              ^^^^^^^^^^
   ╵
[WARNING] drift_dev on lib/app/db/tables/billing.dart:
line 91, column 39 of package:min_budget_flutter/app/db/tables/billing.dart: `CategoryTable` is not a class!
   ╷
91 │       integer().nullable().references(CategoryTable, #id)();
   │                                       ^^^^^^^^^^^^^
   ╵

I created a base table with some public fields. Then use other tables to inherit it, but when I do dart run build_runner watch it reports an error.

The generated datebase.g.dart has only the following content:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'database.dart';

// ignore_for_file: type=lint
abstract class _$AppDatabase extends GeneratedDatabase {
  _$AppDatabase(QueryExecutor e) : super(e);
  @override
  Iterable<TableInfo<Table, Object?>> get allTables =>
      allSchemaEntities.whereType<TableInfo<Table, Object?>>();
  @override
  List<DatabaseSchemaEntity> get allSchemaEntities => [];
}
disonwei commented 1 month ago
➜ dart run build_runner watch
[INFO] Generating build script completed, took 201ms
[INFO] Setting up file watchers completed, took 5ms
[INFO] Waiting for all file watchers to be ready completed, took 200ms
[INFO] Reading cached asset graph completed, took 247ms
[INFO] Checking for updates since last build completed, took 1.0s
[INFO] Running build completed, took 31ms
[INFO] Caching finalized dependency graph completed, took 128ms
[INFO] Succeeded after 163ms with 0 outputs (0 actions)
[INFO] ------------------------------------------------------------------------
[INFO] Starting Build
[INFO] Updating asset graph completed, took 9ms
[WARNING] drift_dev on lib/app/db/database.dart:
line 36, column 7 of package:min_budget_flutter/app/db/database.dart: Could not read tables from @DriftDatabase annotation! 
Please make sure that all table classes exist.
   ╷
36 │ class AppDatabase extends _$AppDatabase {
   │       ^^^^^^^^^^^
   ╵
[INFO] Running build completed, took 11.7s
[INFO] Caching finalized dependency graph completed, took 139ms
[INFO] Succeeded after 11.9s with 203 outputs (403 actions)