Sidharth Samant
Sidharth Samant

Reputation: 786

get() in Google Datastore doesn't work as intended

I'm building a basic blog from the Web Development course by Steve Hoffman on Udacity. This is my code -

import os
import webapp2
import jinja2

from google.appengine.ext import db

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)

def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
    return value.strftime(format)

jinja_env.filters['datetimeformat'] = datetimeformat

def render_str(template, **params):
    t = jinja_env.get_template(template)
    return t.render(params)

class Entries(db.Model):
    title = db.StringProperty(required = True)
    body = db.TextProperty(required = True)
    created = db.DateTimeProperty(auto_now_add = True)

class MainPage(webapp2.RequestHandler):
    def get(self):
        entries = db.GqlQuery('select * from Entries order by created desc limit 10')
        self.response.write(render_str('mainpage.html', entries=entries))

class NewPost(webapp2.RequestHandler):
    def get(self):
        self.response.write(render_str('newpost.html', error=""))

    def post(self):
        title = self.request.get('title')
        body = self.request.get('body')

        if title and body:
            e = Entries(title=title, body=body)
            length = db.GqlQuery('select * from Entries order by created desc').count()
            e.put()
            self.redirect('/newpost/' + str(length+1))
        else:
            self.response.write(render_str('newpost.html', error="Please type in a title and some content"))

class Permalink(webapp2.RequestHandler):
    def get(self, id):
        e = db.GqlQuery('select * from Entries order by created desc').get()
        self.response.write(render_str('permalink.html', id=id, entry = e))

app = webapp2.WSGIApplication([('/', MainPage),
                            ('/newpost', NewPost),
                            ('/newpost/(\d+)', Permalink)
                            ], debug=True)

In the class Permalink, I'm using the get() method on the query than returns all records in the descending order of creation. So, it should return the most recently added record. But when I try to add a new record, permalink.html (it's just a page with shows the title, the body and the date of creation of the new entry) shows the SECOND most recently added. For example, I already had three records, so when I added a fourth record, instead of showing the details of the fourth record, permalink.html showed me the details of the third record. Am I doing something wrong?

I don't think my question is a duplicate of this - Read delay in App Engine Datastore after put(). That question is about read delay of put(), while I'm using get(). The accepted answer also states that get() doesn't cause any delay.

Upvotes: 2

Views: 198

Answers (1)

Alexander Trakhimenok
Alexander Trakhimenok

Reputation: 6268

This is because of eventual consistency used by default for GQL queries.

You need to read:

You can specify read_policy=STRONG_CONSISTENCY for your query but it has associated costs that you should be aware of and take into account.

Upvotes: 2

Related Questions