Skip to content

Is datastore.createQuery.hasAncestor() just work alone without any other query command like order sort or select? #803

Description

@petersunquest

Hi,

I use the [email protected] & nodejs 0.12.7.
And use datastore.createQuery get some result.
Every looks great when I do a query with filter + sort + select + limit + order same time.
But I got Error 412 'Precondition Failed' when I add a hasAncestor() for any filter + sort + select + limit. Even I change order for each one when createQuery.
I used hasAncestor() whthout other filter or sort was work fine.
Is the hasAncestor() can only alone, can't use other command as sort or select or filter same time?
Or that is a bug?
I did:
var q = ds.createQuery('table')
.hasAncestor(ds.key({path:['fatherTable','fatherName']}))
.select('time')
.order('-time')
.limit(5)

Activity

  1. stephenplusplus commented on Aug 17, 2015

    @stephenplusplus
    Contributor

    Thanks for reporting! I'm not sure in this case, but I bet @pcostell can help.

  2. pcostell commented on Aug 17, 2015

    @pcostell
    Contributor

    In order to use hasAncestor with an order you'll need to build an index. If you try running that query while using the gcd.sh tool, it should recommend the proper index. You can learn more about the index config and gcd tool indexes and the gcd tool in general.

    In this particular case, your datastore-indexes.xml file should look something like:

    <?xml version="1.0" encoding="utf-8"?>
    <datastore-indexes autoGenerate="true">
      <datastore-index kind="table" ancestor="true">
        <property name="time" direction="desc" />
      </datastore-index>
    </datastore-indexes>
    
  3. petersunquest commented on Aug 17, 2015

    @petersunquest
    Author

    Hi Pcostell,

    Thank you so much for quickly reply.
    I got it.
    You are right, I did not ready the indexes.xml file.
    The Datastore is so magical, different than SQL. I like that idea to storage data.
    ALso thank you Stephenplusplus.
    Peter

  4. petersunquest commented on Aug 17, 2015

    @petersunquest
    Author

    HI,

    I try add a indexes.xml for my project, Because I use the Goole App with Node.js, so It looks hard to add a indexes.xml file to application's dataset directory. Because if you use gcloud command to push to remote. I try add the Index.xml in my local node project's directory like /war/WEB-INF/appengine-generated/datastore-indexes.xml, then push to remote. Finally it looks not working.
    So it looks just one way that is use the gcd tool.

    1. Download gcd-v1beta2-rev1-2.1.1.zip
    2. #unzip gcd-v1beta2-rev1-2.1.1.zip <------ use terminal command, if not you will get 'appengine-tools-api.jar not found' ERROR
    3. #gcd.sh create -d my-dataset my-project <---- use gcd command to create local dadastore
    4. Than you can check the directory my-project/WEB-INF/ have some file
    5. Add your index file to that WEB-INF/datastore-indexes.xml
    6. #gcd.sh updateindexes --auth_mode=oauth2 my-project <--- then push local datastore setup to remote project
    7. open Developers console web page. check the indexed at Storage->Cloud Datastore->Indexes. It show a index detail.
      Then all looks fine.
      My problem looks clear.

    My question is why let us do so hard work for add a index. (SQL don't need do).
    May be make a 'New index' on Developers console's Datastore.Indexes. You can edit or create a new index with webpage, because some case we don't use Python or Java.

    I very love use Datastore and need clean up all my SQL idea. I hope it will easy to use and learn.

  5. pcostell commented on Aug 17, 2015

    @pcostell
    Contributor

    @petersunquest sorry it's not super easy right now. We are working to integrate gcd into the gcloud command line tool, which will make this much nicer. In the future we also plan to make index creation available via an API, so you should get the "New Index" option in the developer console.

    Unlike SQL, Datastore requires that all queries scale with the size of the result set. Because of this, specific indexes are required to actually run a query. For example, if you were filtering for x = 2, but had no indexes and billions of entities, you would need to scan every single entity looking for entities that match that filter. With Datastore, we require an index on x so that all the results for your query x = 2 exist in a single range scan of the index. Note that if you were running SQL this is still a real concern (which is why you can build indexes), SQL is just willing to issue the expensive (time and cost) query to get your results.

  6. added a commit that references this issue on Nov 10, 2022
  7. added a commit that references this issue on Feb 3, 2026
  8. added a commit that references this issue on Feb 25, 2026
  9. added a commit that references this issue on Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: datastoreIssues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions