Skip to content

Firebase Firestore errorFAILED_PRECONDITION: The query requires an index

Firestore can only run a query that has an index behind it, and this one doesn’t have it yet. The fix itself is short; the index then needs time to build. Here’s how, and how to make sure it ships to production too.

Short answer

A query that combines a filter on one field with a sort or range on another needs a composite index, and Firestore doesn’t create those for you. The full error message includes a link that opens the Firebase console with the index filled in: create it, then wait for it to build (a few minutes at least, longer on big collections). Then add it to firestore.indexes.json and deploy it, so every project has it.

The usual causes, and the fix

  1. 01

    A query on more than one field

    Firestore builds an index for each single field automatically, which covers simple queries. Combine a filter on one field with a sort or range on another, and it needs a composite index on both fields.

    // needs a composite index on (ownerId, createdAt)
    query(collection(db, 'orders'),
      where('ownerId', '==', user.uid),
      orderBy('createdAt', 'desc'));
  2. 02

    You can’t see the link

    The link to create the index is part of the error message. If your app catches the error and shows something generic, log the full error object, or run the query once in development with the console open. Copy the whole link, including everything after create_composite=.

  3. 03

    The index is still building

    Building an index takes a few minutes even for an empty database, and longer the more documents it covers. Until it finishes, the query keeps failing. The Indexes tab in the Firebase console shows the status.

  4. 04

    It exists in one project but not the other

    Indexes belong to a project. One created by clicking the link in your development project doesn’t exist in production. Keep every index in firestore.indexes.json and deploy it with the Firebase CLI; if you add one in the console, copy it into the file too. firebase firestore:indexes prints the project’s indexes so you can compare.

    firebase deploy --only firestore

Checked against the vendor’s docs on : Firebase docs: Manage indexes in Cloud Firestore; Firebase docs: Index types in Cloud Firestore.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: Firebase “Missing or insufficient permissions” · How to cut your Firebase bill

Questions

More on this error

Should I add an index for every field?

No. Single-field indexes are already there. Add composite indexes only for the queries your app actually runs; the error tells you exactly which ones are missing.

Does firebase deploy --only firestore change my rules too?

Yes. It deploys the Security Rules and the indexes from your local files together. Before you run it, check that your local rules file matches what’s live, or you replace the live rules with an older copy.

Why does the same query work in one environment but not another?

Each Firebase project has its own indexes. Deploy firestore.indexes.json to every project the app uses, including staging.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; we reply by email, usually within one business day.