Skip to content

"include" query returning empty array when accessing field #1505

@Jaime02

Description

@Jaime02

Describe the bug
I am trying to use an include subquery in my query.
The query seems to work fine, console.log shows that the productionOrder[0] has the part array (it is a 1:1 relation but currently using toArray is the only way of materializing includes) with the correct part found, but when I access the productionOrders[0].part to get the array (then I will access the part[0] to get the part), it is empty, this should be impossible, it does not make any sense 🤯🤯🤯

To Reproduce

  1. Create a Live Query with an include, a 1:1 relation. In my case:
export function useQueryProductionOrderTable() {
  return useLiveQuery((q) =>
    q.from({ productionOrder: productionOrderCollection }).select(({ productionOrder }) => ({
      ...productionOrder,
      ...
      part: toArray(
        q
          .from({ part: partCollection })
          .where(({ part }) => eq(part.id, productionOrder.partId))
          .select(({ part }) => ({
            ...part,
            shape: toArray(q.from({ shape: shapeCollection }).where(({ shape }) => eq(shape.id, part.shapeId))),
            ),
            ...
          })),
      ),
     ...
    })),
  );
}
  1. Execute query
const { data: productionOrders } = useQueryProductionOrderTable();
console.log("productionOrders[0]:", productionOrders[0]); // This shows that there is a "part" attribute which is an array WITH 1 ELEMENT
console.log("productionOrders[0].part:", productionOrders[0]?.part);  // THIS LOGS AN EMPTY ARRAY ????????

Expected behavior
The query should return the linked part to the productionOrder properly due to the toArray call to materialize the collection instance

Screenshots

Image
  • Device: [e.g. iPhone6]
  • OS: Windows
  • Browser: Brave
  • Version
    @tanstack/electric-db-collection: 0.3.3
    @tanstack/react-db: 0.1.83

STACKBLITZ REPRODUCIBLE EXAMPLE:
https://stackblitz.com/~/github.com/Jaime02/tanstack-db-query-include

I guess that toArray return type should include undefined because that is what is returned while the included collection is loading

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