Skip to content

Commit 18cede1

Browse files
committed
Fix warning for view referencing same table twice
1 parent 5bc2f32 commit 18cede1

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

drift_dev/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.25.3-dev
2+
3+
- Fix warning on Dart-defined views referencing the same table multiple times.
4+
15
## 2.25.2
26

37
- Fix `generateInsertable: true` on `@UseRowClass` not respecting inherited

drift_dev/lib/src/analysis/driver/driver.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class DriftAnalysisDriver {
5656
final DriftBackend backend;
5757
final DriftAnalysisCache cache = DriftAnalysisCache();
5858
final DriftOptions options;
59-
final bool _isTesting;
59+
final bool isTesting;
6060

6161
Future<KnownDriftTypes?>? _loadingTypes;
6262

@@ -65,8 +65,8 @@ class DriftAnalysisDriver {
6565
DriftAnalysisDriver(
6666
this.backend,
6767
this.options, {
68-
bool isTesting = false,
69-
}) : _isTesting = isTesting;
68+
this.isTesting = false,
69+
});
7070

7171
SqlEngine newSqlEngine() {
7272
return SqlEngine(
@@ -272,7 +272,7 @@ class DriftAnalysisDriver {
272272
if (e is! CouldNotResolveElementException) {
273273
backend.log.warning('Could not analyze $id', e, s);
274274

275-
if (_isTesting) rethrow;
275+
if (isTesting) rethrow;
276276
}
277277

278278
return null;

drift_dev/lib/src/analysis/resolver/resolver.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class DriftResolver {
4949
}
5050
} on CouldNotDeserializeException catch (e, s) {
5151
driver.backend.log.warning('Could not deserialize $element', e, s);
52+
if (driver.isTesting) {
53+
rethrow;
54+
}
5255
}
5356

5457
// We can't resolve the element from cache, so we need to resolve it.

drift_dev/lib/src/analysis/serializer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ class ElementDeserializer {
667667
} else if (sourceKind == 'dart') {
668668
TableReferenceInDartView readReference(Map json) {
669669
final id = DriftElementId.fromJson(json['table'] as Map);
670-
final reference = references.singleWhere((e) => e.id == id);
670+
final reference = references.firstWhere((e) => e.id == id);
671671
return TableReferenceInDartView(
672672
reference as DriftTable, json['name'] as String);
673673
}

drift_dev/test/analysis/resolver/dart/view_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,32 @@ abstract class CommonNames extends View {
188188
'''))
189189
}, result.dartOutputs, result.writer);
190190
});
191+
192+
test('can use views referencing same table multiple times', () async {
193+
await emulateDriftBuild(
194+
inputs: {
195+
'a|lib/a.dart': '''
196+
import 'package:drift/drift.dart';
197+
198+
class Users extends Table {
199+
IntColumn get id => integer().autoIncrement()();
200+
TextColumn get name => text()();
201+
}
202+
203+
class UsersView extends View {
204+
Users get a;
205+
Users get b;
206+
207+
@override
208+
Query as() => select([a.id, b.name])
209+
.from(a)
210+
.join([
211+
innerJoin(b, b.name.equalsExp(a.id))
212+
]);
213+
}
214+
''',
215+
},
216+
logger: loggerThat(neverEmits(anything)),
217+
);
218+
});
191219
}

0 commit comments

Comments
 (0)