Skip to content

QueryCompilationError: Input for alias not found when subquery is optimized away #683

@kumpelstachu

Description

@kumpelstachu
  • I've validated the bug against the latest version of DB packages

Describe the bug

QueryCompilationError: Input for alias "parent" (collection "items") not found in inputs map. Available keys: collection

Happens when subquery can be optimized away to just its source collection and join source alias is different than subquery from alias

To Reproduce (example)
Steps to reproduce the behavior:

  1. Create collection:
const collection = createCollection(
	localOnlyCollectionOptions({
		id: 'items',
		getKey: item => item.id,
		schema: z.object({
			id: z.int(),
			parentId: z.int().nullable(),
			name: z.string(),
		}),
	})
)
  1. Use useLiveQuery with a simple subquery:
const { data } = useLiveQuery(q => {
	const subquery = q.from({ collection })
	return q
		.from({ collection })
		.join({ parent: subquery }, ({ parent, collection }) =>
			eq(parent.id, collection.parentId)
		)
})
  1. Observe the error in console or logs:
QueryCompilationError: Input for alias "parent" (collection "items") not found in inputs map. Available keys: collection

Expected behavior
The subquery should resolve correctly even when the optimizer collapses it to its source collection. The join alias should remain valid and consistent.

Additional context
It doesn’t have to be a self-join - the same issue occurs with any subquery that’s optimized away to its base collection while using a different alias in the join.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions