Skip to content

Queries on date has a mutating impact on other queries #616

Description

@fuegoio

Hello again 👋

I have a bug where I use an orderBy and a limit to get most recent items by a date. But weirdingly, when there is another query before this one that just filters on this date, the result is different and wrong.

Codesandbox that reproduces the issue: https://codesandbox.io/p/devbox/quirky-rui-mh7ypn

Minimal reproduction code:

export const numbersCollection = createCollection(
  localOnlyCollectionOptions({
    id: "dates",
    getKey: (item) => item.id,
    initialData: [
      {
        id: "1",
        date: new Date("2025-09-15"),
      },
      {
        id: "2",
        date: new Date("2025-09-10"),
      },
    ],
  })
);

export default function App() {
  const today = new Date();
  // Comment this query and the result will be different
  const firstQueryThatPoisonTheRest = useLiveQuery((q) => {
    return q
      .from({ numbers: numbersCollection })
      .where(({ numbers }) =>
        and(gt(numbers.date, today), lt(numbers.date, today))
      );
  });

  const query = useLiveQuery((q) =>
    q
      .from({ numbers: numbersCollection })
      .orderBy(({ numbers }) => numbers.date, "desc")
      .limit(1)
  );

  return (
    <div className="App">
      <h1>Id: {query.data.at(0).id}</h1>
    </div>
  );
}

When the firstQueryThatPoisonTheRest is commented, the result is Id: 1 which is what we want ("2025-09-15" > "2025-09-10"). But when this query runs first, the result is Id: 2, which is wrong.

Happy to help on this if needed, and thanks again for your work!

Activity

  1. KyleAMathews commented on Sep 30, 2026

    @KyleAMathews
    Collaborator

    Thanks for the concrete reproduction and the CodeSandbox. I retested this on current main at adee4592c41bea9ccf5e817ed8fd1d61fc4eb479 through React's useLiveQuery.

    I used the two Date rows and the same first query with and(gt(date, today), lt(date, today)), followed by orderBy(date, 'desc').limit(1). With and without the first query, and with and without React StrictMode, every ready render returned id 1; the first query returned no rows. I also controlled source timing: I published id 2 and marked the source ready, then published id 1 in a later sync transaction. The ordered result correctly changed from id 2 to id 1 in both query configurations. Six focused React tests passed (pnpm exec vitest --run tests/issue-616-probe.test.tsx), with no type errors.

    I can't reproduce the persistent wrong id 2 on this commit. This test did not run the original CodeSandbox's dependency versions or browser environment. I'll close the issue for now. If it still happens on a current release, please share the package versions and the smallest failing fixture or source event sequence, and we can reopen it. Thanks again for reporting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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