mirror of https://github.com/AMT-Cheif/drift.git
Fix column references in subquery (#1894)
This commit is contained in:
parent
4af9aed190
commit
81c59c0b99
|
@ -22,6 +22,10 @@ class _ExistsExpression<T> extends Expression<bool> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void writeInto(GenerationContext context) {
|
void writeInto(GenerationContext context) {
|
||||||
|
final outerHasMultipleTables = context.hasMultipleTables;
|
||||||
|
// Inside this subquery, we want to reference columns with their table
|
||||||
|
// to avoid ambiguities when an outer table is referenced.
|
||||||
|
context.hasMultipleTables = true;
|
||||||
if (_not) {
|
if (_not) {
|
||||||
context.buffer.write('NOT ');
|
context.buffer.write('NOT ');
|
||||||
}
|
}
|
||||||
|
@ -30,6 +34,7 @@ class _ExistsExpression<T> extends Expression<bool> {
|
||||||
context.buffer.write('(');
|
context.buffer.write('(');
|
||||||
_select.writeInto(context);
|
_select.writeInto(context);
|
||||||
context.buffer.write(')');
|
context.buffer.write(')');
|
||||||
|
context.hasMultipleTables = outerHasMultipleTables;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
|
@ -13,7 +13,7 @@ void main() {
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
existsExpression,
|
existsExpression,
|
||||||
generates('EXISTS (SELECT * FROM users WHERE is_awesome = ?)', [1]),
|
generates('EXISTS (SELECT * FROM users WHERE users.is_awesome = ?)', [1]),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
import 'package:drift/drift.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
import '../generated/todos.dart';
|
||||||
|
import '../test_utils/test_utils.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('exists subqueries properly reference columns', () async {
|
||||||
|
final db = TodoDb.connect(testInMemoryDatabase());
|
||||||
|
addTearDown(db.close);
|
||||||
|
|
||||||
|
final nonEmptyId = await db.categories
|
||||||
|
.insertOne(CategoriesCompanion.insert(description: 'category'));
|
||||||
|
await db.todosTable.insertOne(TodosTableCompanion.insert(
|
||||||
|
content: 'entry', category: Value(nonEmptyId)));
|
||||||
|
|
||||||
|
final emptyId = await db.categories.insertOne(
|
||||||
|
CategoriesCompanion.insert(description: 'this category empty YEET'));
|
||||||
|
|
||||||
|
final emptyCategories = await db.emptyCategories();
|
||||||
|
|
||||||
|
expect(emptyCategories, hasLength(1));
|
||||||
|
expect(emptyCategories.single.id, emptyId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
extension on TodoDb {
|
||||||
|
Future<List<Category>> emptyCategories() {
|
||||||
|
final hasNoTodo = notExistsQuery(select(todosTable)
|
||||||
|
..where((row) => row.category.equalsExp(categories.id)));
|
||||||
|
return (select(categories)..where((row) => hasNoTodo)).get();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue