1
votes

I have created an Entity, called "Event" in the Google Cloud Datastore for my project. The entity has an ID generated by AppEngine followed by two properties

  • Location
  • Date

I am trying to query this entity by its ID (5629499534213120), so , here is my code.

    key = 5629499534213120
    e = Event.get_by_id(key)
    logging.info("Event Location = %s" % e.Location)

The value of e is NoneType.

code

   __author__ = 'vinayjoseph'

from google.appengine.ext import ndb
import logging


class Event(ndb.Model):
    """Models an individual event at xxx xxxx """
    Date = ndb.DateTimeProperty()
    Location = ndb.StringProperty()


def get_meeting_date():
    """gets the next meeting date from the No SQL Schemaless Google Datastore
    """
    key = 5629499534213120
    e = Event.get_by_id(key)
    logging.info("Event Location = %s" % e.Location)

e is NoneType

In the datastore I see the following at https://console.developers.google.com/project/apps~xxxxx/datastore/editentity?key=xxxxx%2FEvent%20id:5629499534213120

screen shot of the entity in the datastore

I suspect the problem might be with my key.

When I try to query the datastore in development using dev_appserver.py it works. I am using a different key for dev.

def get_meeting_date():
    """gets the next meeting date from the No SQL Schemaless Google Datastore
    """
    #dev
    key = 6401356696911872
    #prd
    #key = 5629499534213120
    e = Event.get_by_id(key)
    logging.info("Event Location = %s" % e.Location)

enter image description here

2
Try adding quotes around the ID event_key = ndb.Key("Event", "5629499534213120") - Ryan
ID should be int int, so that should be fine. Does this entity have a parent entity? - GAEfan
Tried both and it did not work And no it does not have a parent entity - Vinay Joseph

2 Answers

2
votes

OK so I finally figured it out.

I had to make a few changes to the entity itself, just to get the filtering right. So the new properties are as seen below. Entity Properties

The code in python is as follows:

__author__ = 'vinayjoseph'

from google.appengine.ext import ndb
import logging
from datetime import datetime

class Event(ndb.Model):
    """Models an individual event at xxx xxx """
    Date = ndb.DateTimeProperty()
    Location = ndb.StringProperty()
    Address = ndb.StringProperty()
    Name = ndb.StringProperty()


def get_meeting_date():
    """gets the next meeting date from the No SQL Schemaless Google Datastore
    """
    qry = Event.query(Event.Name == 'Next Meeting Location')
    for event in qry.fetch(1):
        logging.info("Meeting is on %s at %s" % (str(event.Date), event.Address))

And it works like a charm. Check out the log entry in app-engine

enter image description here

0
votes

In the query method, you're using ancestor. If your entities have a parent, then you have to include it in the get_by_id call.