Open AlexandreAndrade00 opened 1 month ago
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?
That is what I had in mind. But something isn't right in my end :(
What is available:
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);
}
},
);
}
}
Maybe it is because my pks and fks use TypeConvertes instead of being literals (e.g. int, as in your snippet)?
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?
Works for me.
What's the output of dart pub deps
I think the versions are messed up
was this due to concerns about == on the Dart value?
This is a good point, I never considered this...
I'm having trouble reproducing this.
Create a new file named db.dart
and see if this still happens
Works for me as well. @AlexandreAndrade00, can you try to reproduce this with a self-contained example that we could look at?
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
@simolus3
was this due to concerns about == on the Dart value?
Not a concern
In the documentation it shows that the referenced columns should be available when using
withReferences()
, but in my case is not showing up.Tables:
Query: