Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AhmedLSayed9 opened this issue Jan 27, 2025 · 1 comment
Labels
area-manager Related to the manager, exposing an easier to use interface bug Something isn't working generator Affects the generator

Comments

@AhmedLSayed9
Copy link
Contributor

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
@simolus3
Copy link
Owner

I've "improved" this in 5767b8c to add the relevant type arguments so that this is no longer a dynamic invocation. That makes this runtime error a compile-time error - still not great, but it also improves performance when it works.

Refactoring this a bit harder than the forward relations, I'll have to think about this some more.

@simolus3 simolus3 added bug Something isn't working generator Affects the generator area-manager Related to the manager, exposing an easier to use interface labels Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-manager Related to the manager, exposing an easier to use interface bug Something isn't working generator Affects the generator
Projects
None yet
Development

No branches or pull requests

2 participants