drift icon indicating copy to clipboard operation
drift copied to clipboard

Referenced tables not showing in table manager

Open AlexandreAndrade00 opened this issue 1 year ago • 11 comments

In the documentation it shows that the referenced columns should be available when using withReferences(), but in my case is not showing up.

Tables:

class DynamicRelations extends Table {
  Int64Column get id => int64().map(const TsidTypeConverter())();

  Int64Column get sourcePropertyId => int64()
      .map(const TsidTypeConverter())
      .references(DynamicTableProperties, #id)();

  Int64Column get targetPropertyId => int64()
      .map(const TsidTypeConverter())
      .references(DynamicTableProperties, #id)();

  TextColumn get relationType =>
      text().map(const EnumConverter(RelationType.values))();

  DateTimeColumn get creationDate =>
      dateTime().withDefault(currentDateAndTime)();

  @override
  String get tableName => 'dynamic_database__dynamic_relations';

  @override
  Set<Column<Object>>? get primaryKey => {id};
}

class DynamicTableProperties extends Table {
  Int64Column get id => int64().map(const TsidTypeConverter())();

  TextColumn get name => text()();

  Int64Column get tableId =>
      int64().map(const TsidTypeConverter()).references(DynamicTables, #id)();

  @override
  Set<Column<Object>>? get primaryKey => {id};

  @override
  List<Set<Column<Object>>>? get uniqueKeys => [
        {name, tableId}
      ];

  @override
  String get tableName => 'dynamic_database__properties_names';
}

Query:

Future<DynamicRelationProperty> getRelation(Tsid relationId) =>
      relationsManager
          .withReferences()
          .filter((f) => f.id.equals(relationId))
          .getSingle().then((value) => value.$2./*none reference is available*/,);
drift: 2.20.0

AlexandreAndrade00 avatar Oct 02 '24 10:10 AlexandreAndrade00

Your example isn't self-contained, so I've used this modified version
import 'package:drift/drift.dart';
import 'package:drift/native.dart';

part 'repro.g.dart';

class DynamicRelations extends Table {
  IntColumn get id => integer()();
  @ReferenceName('source')
  IntColumn get sourcePropertyId =>
      integer().references(DynamicTableProperties, #id)();
  @ReferenceName('target')
  IntColumn get targetPropertyId =>
      integer().references(DynamicTableProperties, #id)();

  @override
  String get tableName => 'dynamic_database__dynamic_relations';

  @override
  Set<Column<Object>>? get primaryKey => {id};
}

class DynamicTableProperties extends Table {
  IntColumn get id => integer()();
  TextColumn get name => text()();
  IntColumn get tableId => integer().references(DynamicTables, #id)();

  @override
  Set<Column<Object>>? get primaryKey => {id};

  @override
  List<Set<Column<Object>>>? get uniqueKeys => [
        {name, tableId}
      ];

  @override
  String get tableName => 'dynamic_database__properties_names';
}

class DynamicTables extends Table {
  IntColumn get id => integer()();
}

@DriftDatabase(tables: [
  DynamicTables,
  DynamicRelations,
  DynamicTableProperties,
])
class Database extends _$Database {
  Database() : super(NativeDatabase.memory());

  @override
  int get schemaVersion => 1;

  Future<DynamicTableProperty> getRelation(int relationId) =>
      managers.dynamicRelations
          .withReferences()
          .filter((f) => f.id.equals(relationId))
          .getSingle()
          .then((value) => value.$2.sourcePropertyId!.getSingle());

  Future<DynamicTableProperty> getRelationWithPrefetch(int relationId) =>
      managers.dynamicRelations
          .withReferences((prefetch) => prefetch(sourcePropertyId: true))
          .filter((f) => f.id.equals(relationId))
          .getSingle()
          .then((value) => value.$2.sourcePropertyId!.prefetchedData!.first);
}

void main() async {
  final db = Database();
  await db.dynamicTables.insertOne(DynamicTablesCompanion.insert(id: 1));
  await db.dynamicTableProperties.insertOne(
      DynamicTablePropertiesCompanion.insert(name: 'source', tableId: 1));
  await db.dynamicTableProperties.insertOne(
      DynamicTablePropertiesCompanion.insert(name: 'target', tableId: 1));
  await db.dynamicRelations.insertOne(DynamicRelationsCompanion.insert(
      sourcePropertyId: 1, targetPropertyId: 2));

  print(await db.getRelation(1));
  print(await db.getRelationWithPrefetch(1));
}

I'm not sure what relationsManager is, but managers.dynamicRelations in the database with references generates targetPropertyId and sourcePropertyId under the references field of the record:

Future<DynamicTableProperty> getRelation(int relationId) =>
    managers.dynamicRelations
        .withReferences()
        .filter((f) => f.id.equals(relationId))
        .getSingle()
        .then((value) => value.$2.sourcePropertyId!.getSingle());

You can also use prefetching to add an initial join that avoids the second query:

Future<DynamicTableProperty> getRelationWithPrefetch(int relationId) =>
    managers.dynamicRelations
        .withReferences((prefetch) => prefetch(sourcePropertyId: true))
        .filter((f) => f.id.equals(relationId))
        .getSingle()
        .then((value) => value.$2.sourcePropertyId!.prefetchedData!.first);

Is that the query you had in mind?

simolus3 avatar Oct 02 '24 15:10 simolus3

That is what I had in mind. But something isn't right in my end :(

What is available: image

more context:

@DriftAccessor(tables: [
  DynamicTables,
  DynamicRelations,
  DynamicTableProperties,
])
class DynamicDatabaseDao extends DatabaseAccessor<Database>
    with $DynamicDatabaseDaoMixin {
  DynamicDatabaseDao(super.attachedDatabase);

  $$DynamicRelationsTableTableManager get relationsManager =>
      attachedDatabase.managers.dynamicRelations;

  Future<DynamicTableProperty> getRelation(Tsid relationId) =>
      relationsManager
          .withReferences()
          .filter((f) => f.id.equals(relationId))
          .getSingle().then((value) => value.$2./*none reference is available*/,);
}

Database class:

@DriftDatabase(tables: [
  ArtifactCoordinates,
  ArtifactReferences,
  DynamicTables,
  DynamicColumns,
  Assets,
  NavigationBars,
  NavigationEntries,
  Tags,
  DynamicTableDataSources,
  SchemaDataSources,
  Screens,
  ScreenContainers,
  Elements,
  DynamicTableForeignKeys,
  ContainerObjects,
  Frames,
  Animations,
  ExerciseContainers,
  Fields,
  PlayFields,
  Projects,
  FrameObjects,
  ChangeLogs,
  DynamicRelations,
  TagsHierarchies,
  TagsHierarchiesMapping,
  MAProjects,
  Annotations,
  MATagsHierarchies,
  VideoCutTagHierarchies,
  VideoFiles,
  VideoCuts,
  VideoCutTags,
  DynamicMultipleOptions,
  DynamicMultipleOptionsMapping,
  DynamicReferencesMapping,
  Users,
  FunctionalGroups,
  FunctionalRoles,
  UserToFunctionalRoles,
  UserToFunctionalRoleToFunctionalGroups,
  FunctionalGroupToFunctionalRoles,
  ArtifactPermissions,
  ModuleApplicationPermissions,
  DynamicTableProperties,
], daos: [
  DynamicDatabaseDao,
  ReferencesManagerDao,
  TagsDao,
  AssetsManagerDao,
  NavigationBarDao,
  DataSourcesDao,
  ScreensDao,
  ExercisesEditorDao,
  MatchAnalysisDao,
  AuthenticationControllerDao,
  PermissionsDao,
  PersistenceDao,
], views: [
  DynamicColumnsWithForeignKeys,
  DynamicColumnsMultipleOptions,
  DynamicColumnProperties,
  DynamicRelationProperties,
])
abstract class Database extends $Database {
  final String databaseName;

  Database({
    required this.databaseName,
    required QueryExecutor executor,
  }) : super(executor);

  @override
  int get schemaVersion => 1;

  // TODO: Add MigrationStrategy when required;
  // See https://github.com/simolus3/drift/blob/develop/examples/migrations_example/lib/database.dart
  @override
  MigrationStrategy get migration {
    return MigrationStrategy(
      beforeOpen: (details) async {
        await customStatement('PRAGMA foreign_keys = ON;');

        final tables = await customSelect("""
          SELECT name
          FROM sqlite_master
          WHERE type='table';
        """).get();

        for (final table in tables) {
          final tableName = table.read<String>("name");

          if (tableName == changeLogsTableName) continue;

          await registerLoggingTriggers(tableName);
        }
      },
    );
  }
}

AlexandreAndrade00 avatar Oct 02 '24 16:10 AlexandreAndrade00

Maybe it is because my pks and fks use TypeConvertes instead of being literals (e.g. int, as in your snippet)?

AlexandreAndrade00 avatar Oct 02 '24 16:10 AlexandreAndrade00

Oh right, good point! I think we disabled references for columns with type converters because there were some issues even if both columns were using the same converter. cc @dickermoshe was this due to concerns about == on the Dart value?

simolus3 avatar Oct 02 '24 16:10 simolus3

Works for me. What's the output of dart pub deps

I think the versions are messed up

dickermoshe avatar Oct 02 '24 18:10 dickermoshe

was this due to concerns about == on the Dart value?

This is a good point, I never considered this...

dickermoshe avatar Oct 02 '24 18:10 dickermoshe

dart pub deps
Dart SDK 3.5.0
Flutter SDK 3.24.0
persistence 1.0.0-alpha.1
├── basic_utils 5.7.0
│   ├── pointycastle 3.9.1
│   │   ├── collection...
│   │   ├── convert...
│   │   └── js...
│   ├── http...
│   ├── json_annotation...
│   └── logging...
├── build_runner 2.4.12
│   ├── analyzer 6.7.0
│   │   ├── _fe_analyzer_shared 72.0.0
│   │   │   └── meta...
│   │   ├── macros 0.1.2-main.4
│   │   │   └── _macros 0.3.2
│   │   ├── collection...
│   │   ├── convert...
│   │   ├── crypto...
│   │   ├── glob...
│   │   ├── meta...
│   │   ├── package_config...
│   │   ├── path...
│   │   ├── pub_semver...
│   │   ├── source_span...
│   │   ├── watcher...
│   │   └── yaml...
│   ├── args 2.5.0
│   ├── build_config 1.1.1
│   │   ├── checked_yaml 2.0.3
│   │   │   ├── json_annotation...
│   │   │   ├── source_span...
│   │   │   └── yaml...
│   │   ├── json_annotation...
│   │   ├── path...
│   │   ├── pubspec_parse...
│   │   └── yaml...
│   ├── build_daemon 4.0.2
│   │   ├── built_collection...
│   │   ├── built_value...
│   │   ├── crypto...
│   │   ├── http_multi_server...
│   │   ├── logging...
│   │   ├── path...
│   │   ├── pool...
│   │   ├── shelf...
│   │   ├── shelf_web_socket...
│   │   ├── stream_transform...
│   │   ├── watcher...
│   │   └── web_socket_channel...
│   ├── build_resolvers 2.4.2
│   │   ├── analyzer...
│   │   ├── async...
│   │   ├── build...
│   │   ├── collection...
│   │   ├── convert...
│   │   ├── crypto...
│   │   ├── graphs...
│   │   ├── logging...
│   │   ├── package_config...
│   │   ├── path...
│   │   ├── pool...
│   │   ├── pub_semver...
│   │   ├── stream_transform...
│   │   └── yaml...
│   ├── build_runner_core 7.3.2
│   │   ├── async...
│   │   ├── build...
│   │   ├── build_config...
│   │   ├── build_resolvers...
│   │   ├── collection...
│   │   ├── convert...
│   │   ├── crypto...
│   │   ├── glob...
│   │   ├── graphs...
│   │   ├── json_annotation...
│   │   ├── logging...
│   │   ├── meta...
│   │   ├── package_config...
│   │   ├── path...
│   │   ├── pool...
│   │   ├── timing...
│   │   ├── watcher...
│   │   └── yaml...
│   ├── frontend_server_client 4.0.0
│   │   ├── async...
│   │   └── path...
│   ├── glob 2.1.2
│   │   ├── file 7.0.0
│   │   │   ├── meta...
│   │   │   └── path...
│   │   ├── async...
│   │   ├── collection...
│   │   ├── path...
│   │   └── string_scanner...
│   ├── graphs 2.3.2
│   │   └── collection...
│   ├── http_multi_server 3.2.1
│   │   └── async...
│   ├── io 1.0.4
│   │   ├── meta...
│   │   ├── path...
│   │   └── string_scanner...
│   ├── mime 1.0.6
│   ├── package_config 2.1.0
│   │   └── path...
│   ├── pool 1.5.1
│   │   ├── async...
│   │   └── stack_trace...
│   ├── pub_semver 2.1.4
│   │   ├── collection...
│   │   └── meta...
│   ├── pubspec_parse 1.3.0
│   │   ├── checked_yaml...
│   │   ├── collection...
│   │   ├── json_annotation...
│   │   ├── pub_semver...
│   │   └── yaml...
│   ├── shelf 1.4.1
│   │   ├── async...
│   │   ├── collection...
│   │   ├── http_parser...
│   │   ├── path...
│   │   ├── stack_trace...
│   │   └── stream_channel...
│   ├── shelf_web_socket 2.0.0
│   │   ├── shelf...
│   │   ├── stream_channel...
│   │   └── web_socket_channel...
│   ├── stream_transform 2.1.0
│   ├── timing 1.0.1
│   │   └── json_annotation...
│   ├── watcher 1.1.0
│   │   ├── async...
│   │   └── path...
│   ├── web_socket_channel 3.0.1
│   │   ├── web_socket 0.1.6
│   │   │   └── web...
│   │   ├── async...
│   │   ├── crypto...
│   │   ├── stream_channel...
│   │   └── web...
│   ├── yaml 3.1.2
│   │   ├── collection...
│   │   ├── source_span...
│   │   └── string_scanner...
│   ├── async...
│   ├── build...
│   ├── code_builder...
│   ├── collection...
│   ├── crypto...
│   ├── dart_style...
│   ├── js...
│   ├── logging...
│   ├── meta...
│   ├── path...
│   └── stack_trace...
├── clock 1.1.1
├── collection 1.18.0
├── core 1.0.0-alpha.6
│   ├── bootstrap_icons 1.11.1
│   │   └── flutter...
│   ├── build 2.4.1
│   │   ├── analyzer...
│   │   ├── async...
│   │   ├── convert...
│   │   ├── crypto...
│   │   ├── glob...
│   │   ├── logging...
│   │   ├── meta...
│   │   ├── package_config...
│   │   └── path...
│   ├── code_builder 4.10.0
│   │   ├── built_collection 5.1.1
│   │   ├── built_value 8.9.2
│   │   │   ├── built_collection...
│   │   │   ├── collection...
│   │   │   ├── fixnum...
│   │   │   └── meta...
│   │   ├── collection...
│   │   ├── matcher...
│   │   └── meta...
│   ├── dio_compatibility_layer 0.1.0
│   │   ├── dio...
│   │   └── http...
│   ├── dotted_border 2.1.0
│   │   ├── path_drawing 1.0.1
│   │   │   ├── path_parsing 1.0.1
│   │   │   │   ├── meta...
│   │   │   │   └── vector_math...
│   │   │   ├── flutter...
│   │   │   ├── meta...
│   │   │   └── vector_math...
│   │   └── flutter...
│   ├── fetch_client 1.1.2
│   │   ├── fetch_api 2.2.0
│   │   │   └── web...
│   │   └── http...
│   ├── flex_color_picker 3.3.0
│   │   ├── flex_seed_scheme 1.5.0
│   │   │   ├── collection...
│   │   │   ├── flutter...
│   │   │   └── meta...
│   │   └── flutter...
│   ├── flutter_bloc 8.1.3
│   │   ├── bloc 8.1.4
│   │   │   └── meta...
│   │   ├── provider 6.1.2
│   │   │   ├── nested 1.0.0
│   │   │   │   └── flutter...
│   │   │   ├── collection...
│   │   │   └── flutter...
│   │   └── flutter...
│   ├── flutter_svg 2.0.7
│   │   ├── vector_graphics 1.1.11+1
│   │   │   ├── flutter...
│   │   │   ├── http...
│   │   │   └── vector_graphics_codec...
│   │   ├── vector_graphics_codec 1.1.11+1
│   │   ├── vector_graphics_compiler 1.1.11+1
│   │   │   ├── xml 6.5.0
│   │   │   │   ├── collection...
│   │   │   │   ├── meta...
│   │   │   │   └── petitparser...
│   │   │   ├── args...
│   │   │   ├── meta...
│   │   │   ├── path...
│   │   │   ├── path_parsing...
│   │   │   └── vector_graphics_codec...
│   │   └── flutter...
│   ├── internet_connection_checker_plus 2.2.0
│   │   ├── connectivity_plus 5.0.2
│   │   │   ├── connectivity_plus_platform_interface 1.2.4
│   │   │   │   ├── flutter...
│   │   │   │   ├── meta...
│   │   │   │   └── plugin_platform_interface...
│   │   │   ├── flutter_web_plugins 0.0.0
│   │   │   │   ├── characters...
│   │   │   │   ├── collection...
│   │   │   │   ├── flutter...
│   │   │   │   ├── material_color_utilities...
│   │   │   │   ├── meta...
│   │   │   │   └── vector_math...
│   │   │   ├── nm 0.5.0
│   │   │   │   └── dbus 0.7.10
│   │   │   │       ├── args...
│   │   │   │       ├── ffi...
│   │   │   │       ├── meta...
│   │   │   │       └── xml...
│   │   │   ├── flutter...
│   │   │   ├── js...
│   │   │   └── meta...
│   │   ├── flutter...
│   │   └── http...
│   ├── material_symbols_icons 4.2758.0
│   │   └── flutter...
│   ├── pretty_dio_logger 1.4.0
│   │   └── dio...
│   ├── shared_preferences 2.3.1
│   │   ├── shared_preferences_android 2.3.2
│   │   │   ├── flutter...
│   │   │   └── shared_preferences_platform_interface...
│   │   ├── shared_preferences_foundation 2.5.2
│   │   │   ├── flutter...
│   │   │   └── shared_preferences_platform_interface...
│   │   ├── shared_preferences_linux 2.4.1
│   │   │   ├── file...
│   │   │   ├── flutter...
│   │   │   ├── path...
│   │   │   ├── path_provider_linux...
│   │   │   ├── path_provider_platform_interface...
│   │   │   └── shared_preferences_platform_interface...
│   │   ├── shared_preferences_platform_interface 2.4.1
│   │   │   ├── flutter...
│   │   │   └── plugin_platform_interface...
│   │   ├── shared_preferences_web 2.4.2
│   │   │   ├── flutter...
│   │   │   ├── flutter_web_plugins...
│   │   │   ├── shared_preferences_platform_interface...
│   │   │   └── web...
│   │   ├── shared_preferences_windows 2.4.1
│   │   │   ├── file...
│   │   │   ├── flutter...
│   │   │   ├── path...
│   │   │   ├── path_provider_platform_interface...
│   │   │   ├── path_provider_windows...
│   │   │   └── shared_preferences_platform_interface...
│   │   └── flutter...
│   ├── source_gen 1.5.0
│   │   ├── analyzer...
│   │   ├── async...
│   │   ├── build...
│   │   ├── dart_style...
│   │   ├── glob...
│   │   ├── path...
│   │   ├── source_span...
│   │   └── yaml...
│   ├── uuid 4.4.0
│   │   ├── fixnum 1.1.0
│   │   ├── sprintf 7.0.0
│   │   ├── crypto...
│   │   └── meta...
│   ├── collection...
│   ├── dio...
│   ├── equatable...
│   ├── flutter...
│   ├── flutter_localizations...
│   ├── get_it...
│   ├── intl...
│   ├── json_annotation...
│   ├── json_bigint...
│   ├── logging...
│   ├── path...
│   ├── path_provider...
│   └── tsid_dart...
├── crypto 3.0.3
│   └── typed_data 1.3.2
│       └── collection...
├── dart_casing 3.0.1
├── dio 5.6.0
│   ├── async 2.11.0
│   │   ├── collection...
│   │   └── meta...
│   ├── dio_web_adapter 2.0.0
│   │   ├── dio...
│   │   ├── http_parser...
│   │   ├── meta...
│   │   └── web...
│   ├── http_parser 4.0.2
│   │   ├── collection...
│   │   ├── source_span...
│   │   ├── string_scanner...
│   │   └── typed_data...
│   ├── meta...
│   └── path...
├── drift 2.20.0
│   ├── convert 3.1.1
│   │   └── typed_data...
│   ├── js 0.6.7
│   │   └── meta...
│   ├── stack_trace 1.11.1
│   │   └── path...
│   ├── stream_channel 2.1.2
│   │   └── async...
│   ├── web 1.1.0
│   ├── async...
│   ├── collection...
│   ├── meta...
│   ├── path...
│   └── sqlite3...
├── drift_dev 2.20.1
│   ├── analyzer_plugin 0.11.3
│   │   ├── analyzer...
│   │   ├── collection...
│   │   ├── dart_style...
│   │   ├── pub_semver...
│   │   └── yaml...
│   ├── charcode 1.3.1
│   ├── cli_util 0.4.1
│   │   ├── meta...
│   │   └── path...
│   ├── recase 4.1.0
│   ├── sqlparser 0.37.1
│   │   ├── charcode...
│   │   ├── collection...
│   │   ├── meta...
│   │   └── source_span...
│   ├── analyzer...
│   ├── args...
│   ├── build...
│   ├── build_config...
│   ├── build_resolvers...
│   ├── collection...
│   ├── dart_style...
│   ├── drift...
│   ├── io...
│   ├── json_annotation...
│   ├── logging...
│   ├── meta...
│   ├── package_config...
│   ├── path...
│   ├── pub_semver...
│   ├── source_gen...
│   ├── source_span...
│   ├── sqlite3...
│   ├── stream_transform...
│   ├── string_scanner...
│   └── yaml...
├── equatable 2.0.5
│   ├── collection...
│   └── meta...
├── eventflux 2.1.0
│   ├── flutter...
│   └── http...
├── flutter 0.0.0
│   ├── characters 1.3.0
│   ├── material_color_utilities 0.11.1
│   │   └── collection...
│   ├── sky_engine 0.0.99
│   ├── vector_math 2.1.4
│   ├── collection...
│   └── meta...
├── flutter_lints 4.0.0
│   └── lints 4.0.0
├── flutter_localizations 0.0.0
│   ├── characters...
│   ├── clock...
│   ├── collection...
│   ├── flutter...
│   ├── intl...
│   ├── material_color_utilities...
│   ├── meta...
│   ├── path...
│   └── vector_math...
├── flutter_test 0.0.0
│   ├── boolean_selector 2.1.1
│   │   ├── source_span...
│   │   └── string_scanner...
│   ├── fake_async 1.3.1
│   │   ├── clock...
│   │   └── collection...
│   ├── leak_tracker 10.0.5
│   │   ├── clock...
│   │   ├── collection...
│   │   ├── meta...
│   │   ├── path...
│   │   └── vm_service...
│   ├── leak_tracker_flutter_testing 3.0.5
│   │   ├── flutter...
│   │   ├── leak_tracker...
│   │   ├── leak_tracker_testing...
│   │   ├── matcher...
│   │   └── meta...
│   ├── leak_tracker_testing 3.0.1
│   │   ├── leak_tracker...
│   │   ├── matcher...
│   │   └── meta...
│   ├── matcher 0.12.16+1
│   │   ├── async...
│   │   ├── meta...
│   │   ├── stack_trace...
│   │   ├── term_glyph...
│   │   └── test_api...
│   ├── source_span 1.10.0
│   │   ├── collection...
│   │   ├── path...
│   │   └── term_glyph...
│   ├── string_scanner 1.2.0
│   │   └── source_span...
│   ├── term_glyph 1.2.1
│   ├── test_api 0.7.2
│   │   ├── async...
│   │   ├── boolean_selector...
│   │   ├── collection...
│   │   ├── meta...
│   │   ├── source_span...
│   │   ├── stack_trace...
│   │   ├── stream_channel...
│   │   ├── string_scanner...
│   │   └── term_glyph...
│   ├── vm_service 14.2.4
│   ├── async...
│   ├── characters...
│   ├── clock...
│   ├── collection...
│   ├── flutter...
│   ├── material_color_utilities...
│   ├── meta...
│   ├── path...
│   ├── stack_trace...
│   ├── stream_channel...
│   └── vector_math...
├── get_it 7.7.0
│   ├── async...
│   ├── collection...
│   └── meta...
├── golden_toolkit 0.15.0
│   ├── flutter...
│   ├── flutter_test...
│   └── meta...
├── http 1.2.2
│   ├── async...
│   ├── http_parser...
│   ├── meta...
│   └── web...
├── intl 0.19.0
│   ├── clock...
│   ├── meta...
│   └── path...
├── json_annotation 4.9.0
│   └── meta...
├── json_bigint 3.0.0
│   ├── petitparser 6.0.2
│   │   └── meta...
│   └── meta...
├── json_serializable 6.8.0
│   ├── source_helper 1.3.4
│   │   ├── analyzer...
│   │   ├── collection...
│   │   └── source_gen...
│   ├── analyzer...
│   ├── async...
│   ├── build...
│   ├── build_config...
│   ├── collection...
│   ├── json_annotation...
│   ├── meta...
│   ├── path...
│   ├── pub_semver...
│   ├── pubspec_parse...
│   └── source_gen...
├── logging 1.2.0
├── meta 1.15.0
├── mocktail 1.0.3
│   ├── collection...
│   ├── matcher...
│   └── test_api...
├── path 1.9.0
├── path_provider 2.1.2
│   ├── path_provider_android 2.2.10
│   │   ├── flutter...
│   │   └── path_provider_platform_interface...
│   ├── path_provider_foundation 2.4.0
│   │   ├── flutter...
│   │   └── path_provider_platform_interface...
│   ├── path_provider_linux 2.2.1
│   │   ├── xdg_directories 1.0.4
│   │   │   ├── meta...
│   │   │   └── path...
│   │   ├── ffi...
│   │   ├── flutter...
│   │   ├── path...
│   │   └── path_provider_platform_interface...
│   ├── path_provider_platform_interface 2.1.2
│   │   ├── platform 3.1.5
│   │   ├── plugin_platform_interface 2.1.8
│   │   │   └── meta...
│   │   └── flutter...
│   ├── path_provider_windows 2.3.0
│   │   ├── ffi...
│   │   ├── flutter...
│   │   ├── path...
│   │   └── path_provider_platform_interface...
│   └── flutter...
├── sqlcipher_flutter_libs 0.6.3
│   └── flutter...
├── sqlite3 2.4.6
│   ├── ffi 2.1.3
│   ├── collection...
│   ├── meta...
│   ├── path...
│   └── web...
├── string_normalizer 0.3.1
│   ├── dart_style 2.3.7
│   │   ├── analyzer...
│   │   ├── args...
│   │   ├── collection...
│   │   ├── package_config...
│   │   ├── path...
│   │   ├── pub_semver...
│   │   └── source_span...
│   ├── characters...
│   └── http...
├── synchronized 3.1.0+1
├── testing_support_toolkit 1.0.0-alpha.1
│   ├── basic_utils...
│   ├── core...
│   ├── flutter...
│   ├── flutter_bloc...
│   ├── flutter_test...
│   ├── golden_toolkit...
│   ├── intl...
│   └── meta...
└── tsid_dart 0.0.5
    ├── decimal 2.3.3
    │   └── rational 2.2.3
    ├── collection...
    └── convert...

AlexandreAndrade00 avatar Oct 02 '24 18:10 AlexandreAndrade00

I'm having trouble reproducing this.

Create a new file named db.dart and see if this still happens

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

part 'db.g.dart';

enum RelationType {
  oneToOne,
  oneToMany,
  manyToOne,
  manyToMany,
}

class Tsid {
  final BigInt value;

  const Tsid(this.value);
}

class TsidTypeConverter extends TypeConverter<Tsid, BigInt> {
  const TsidTypeConverter();

  @override
  Tsid fromSql(BigInt fromDb) {
    return Tsid(fromDb);
  }

  @override
  BigInt toSql(Tsid value) {
    return value.value;
  }
}

@DriftAccessor(tables: [
  DynamicRelations,
  DynamicTableProperties,
])
class DynamicDatabaseDao extends DatabaseAccessor<Database>
    with _$DynamicDatabaseDaoMixin {
  DynamicDatabaseDao(super.attachedDatabase);

  $$DynamicRelationsTableTableManager get relationsManager =>
      attachedDatabase.managers.dynamicRelations;

  Future<DynamicTableProperty?> getRelation(Tsid relationId) => relationsManager
      .withReferences()
      .filter((f) => f.id.equals(relationId))
      .getSingle()
      .then((value) => value.$2.sourcePropertyId?.getSingle());
}

class DynamicRelations extends Table {
  Int64Column get id => int64().map(const TsidTypeConverter())();

  Int64Column get sourcePropertyId => int64()
      .map(const TsidTypeConverter())
      .references(DynamicTableProperties, #id)();

  Int64Column get targetPropertyId => int64()
      .map(const TsidTypeConverter())
      .references(DynamicTableProperties, #id)();

  DateTimeColumn get creationDate =>
      dateTime().withDefault(currentDateAndTime)();

  @override
  String get tableName => 'dynamic_database__dynamic_relations';

  @override
  Set<Column<Object>>? get primaryKey => {id};
}

class DynamicTableProperties extends Table {
  Int64Column get id => int64().map(const TsidTypeConverter())();

  TextColumn get name => text()();

  Int64Column get tableId => int64().map(const TsidTypeConverter())();

  @override
  Set<Column<Object>>? get primaryKey => {id};

  @override
  List<Set<Column<Object>>>? get uniqueKeys => [
        {name, tableId}
      ];

  @override
  String get tableName => 'dynamic_database__properties_names';
}

@DriftDatabase(tables: [
  DynamicRelations,
  DynamicTableProperties,
], daos: [
  DynamicDatabaseDao
], views: [])
abstract class Database extends _$Database {
  final String databaseName;

  Database({
    required this.databaseName,
    required QueryExecutor executor,
  }) : super(executor);

  @override
  int get schemaVersion => 1;
}

dickermoshe avatar Oct 02 '24 19:10 dickermoshe

Works for me as well. @AlexandreAndrade00, can you try to reproduce this with a self-contained example that we could look at?

simolus3 avatar Oct 07 '24 20:10 simolus3

Sorry for the delay :grimacing: As soon as I can I will try to reproduce this with a self-contained example. Thank you for your help :D

AlexandreAndrade00 avatar Oct 07 '24 20:10 AlexandreAndrade00

@simolus3

was this due to concerns about == on the Dart value?

Not a concern

dickermoshe avatar Oct 09 '24 20:10 dickermoshe

Old Issue. Closing for now...

dickermoshe avatar Feb 03 '25 00:02 dickermoshe