drift icon indicating copy to clipboard operation
drift copied to clipboard

Manager references around custom row classes throws an error when calling prefetchedData

Open AhmedLSayed9 opened this issue 10 months ago • 3 comments

Related to #3335

Simply, try running the following sample:

import 'package:drift/drift.dart';
import 'package:drift/native.dart';

part 'main.g.dart';

class AnotherTable extends Table {
  IntColumn get id => integer().autoIncrement()();
}

@UseRowClass(Item)
class Items extends Table {
  IntColumn get id => integer().autoIncrement()();
  IntColumn get anotherTable => integer().references(AnotherTable, #id)();
}

class Item {
  Item({required this.id});

  final int id;
}

@DriftDatabase(tables: [AnotherTable, Items])
class Database extends _$Database {
  Database(super.e);

  @override
  int get schemaVersion => 1;

  @override
  MigrationStrategy get migration => MigrationStrategy();
}

Future<void> main() async {
  final db = Database(NativeDatabase.memory());

  await db.into(db.anotherTable).insert(AnotherTableCompanion.insert(id: const Value(1)));

  await db.into(db.items).insert(ItemsCompanion.insert(id: const Value(1), anotherTable: 1));

  final another = await db.managers.anotherTable
      .withReferences((prefetch) => prefetch(itemsRefs: true))
      .getSingle();

  final item = another.$2.itemsRefs.prefetchedData?.first;
  print(item);
}

The following error will be thrown:

NoSuchMethodError: Class 'Item' has no instance getter 'anotherTable'.
Receiver: Instance of 'Item'
Tried calling: anotherTable

AhmedLSayed9 avatar Jan 27 '25 21:01 AhmedLSayed9