Hi,

I'm having trouble understanding why this fails, any help appreciated:

class Topic(db.Model):
  name=db.StringProperty()

class Greeting(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)
  topic=db.ReferenceProperty(Topic)

running:

    topicList= [e.key() for e in Topic.all().fetch(4)]

    greetings = db.GqlQuery("SELECT * "
                            "FROM Greeting "
                            "WHERE topic in :topics  "
                            "ORDER BY __key__ DESC",topics=topicList)

when there is more than 1 topic in the topicList, gives:

Traceback (most recent call last):
  File "/home/paul/projects/indexing/google/appengine/ext/webapp/
__init__.py", line 498, in __call__
    handler.get(*groups)
  File "/home/paul/projects/indexing/demos/guestbook/guestbook.py",
line 48, in get
    for greeting in greetings:
  File "/home/paul/projects/indexing/google/appengine/ext/db/
__init__.py", line 1456, in next
    return self.__model_class.from_entity(self.__iterator.next())
  File "/home/paul/projects/indexing/google/appengine/ext/gql/
__init__.py", line 1231, in IterateResults
    heapq.heappush(result_heap, heap_value)
  File "/home/paul/projects/indexing/google/appengine/ext/gql/
__init__.py", line 1199, in __cmp__
    property_compare = self.CmpProperties(that)
  File "/home/paul/projects/indexing/google/appengine/ext/gql/
__init__.py", line 1161, in CmpProperties
    value1 = self.__GetValueForId(self, identifier, order)
  File "/home/paul/projects/indexing/google/appengine/ext/gql/
__init__.py", line 1172, in __GetValueForId
    value = sort_order_entity.__entity[identifier]
KeyError: '__key__'

...

Whereas ordering by date as well as __key__:

    greetings = db.GqlQuery("SELECT * "
                            "FROM Greeting "
                            "WHERE topic in :topics  "
                            "ORDER BY date, __key__
DESC",topics=topicList)

creates this index.yaml entry:

# Used 2 times in query history.
- kind: Greeting
  properties:
  - name: topic
  - name: date
    direction: desc
  - name: __key__
    direction: desc


and works OK.

---------------------------------------------------


Full diff from 1.1.8 guestbook.py:


--- demos/guestbook/guestbook.py        (revision 1336)
+++ demos/guestbook/guestbook.py        (working copy)
@@ -22,31 +22,42 @@
 from google.appengine.api import users
 from google.appengine.ext import webapp

+
+class Topic(db.Model):
+  name=db.StringProperty()
+
 class Greeting(db.Model):
   author = db.UserProperty()
   content = db.StringProperty(multiline=True)
   date = db.DateTimeProperty(auto_now_add=True)
+  topic=db.ReferenceProperty(Topic)


 class MainPage(webapp.RequestHandler):
   def get(self):
     self.response.out.write('<html><body>')

+
+    topicList= [e.key() for e in Topic.all().fetch(4)]
+
     greetings = db.GqlQuery("SELECT * "
                             "FROM Greeting "
-                            "ORDER BY date DESC LIMIT 10")
+                            "WHERE topic in :topics  "
+                            "ORDER BY __key__ DESC",topics=topicList)

     for greeting in greetings:
       if greeting.author:
         self.response.out.write('<b>%s</b> wrote:' %
greeting.author.nickname())
       else:
         self.response.out.write('An anonymous person wrote:')
-      self.response.out.write('<blockquote>%s</blockquote>' %
-                              cgi.escape(greeting.content))
+      self.response.out.write('<blockquote>%s : %s</blockquote>' %
+                              (cgi.escape(greeting.topic.name),
+                               cgi.escape(greeting.content)))

     self.response.out.write("""
           <form action="/sign" method="post">
             <div><textarea name="content" rows="3" cols="60"></
textarea></div>
+            <div><textarea name="topic" rows="1" cols="60"></
textarea></div>
             <div><input type="submit" value="Sign Guestbook"></div>
           </form>
         </body>
@@ -61,6 +72,17 @@
       greeting.author = users.get_current_user()

     greeting.content = self.request.get('content')
+
+    topic_name=self.request.get('topic').strip()
+    topicList = Topic.gql("WHERE name = :name",name=topic_name).fetch
(1)
+    topic=None
+    if topicList:
+      topic = topicList[0]
+    else:
+      topic=Topic(name=topic_name)
+      topic.put()
+
+    greeting.topic = topic
     greeting.put()
     self.redirect('/')

----------------------------


submit 2 guestbook entries like:

content1
topic1

content2
topic2

reproduces the issue above.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to