# I believe the new '_last_reference'  property still needs a
'nativeRef' functionality
# added to web2py in order to take full advantage of it:
# I coded and successfully tested a proof of concept as follows:

db.define_table('ParentTable',
               # NOTE: web2py provides the 'id' column automatically
                db.Field('name','string', length=50), # NOTE! for GAE
you must
specify 'string', not 'text'!!
                db.Field('gaeRef', gae.ReferenceProperty())
              )

db.define_table('ChildTable',
               # NOTE: web2py provides the 'id' column automatically
                db.Field('idParentFolder','reference ParentTable'),
                db.Field('age','integer'),
                db.Field('name','string', length=50), # NOTE! for GAE
you must
specify 'string', not 'text'!!
                db.Field('parentLink',
gae.ReferenceProperty(db.ParentTable._tableobj, required=True,
collection_name='ChildCollection')),
                db.Field('gaeRef', gae.ReferenceProperty())
               )

# To support this, I patched gql.py around line 700 in method
'select':
def select(self, *fields, **attributes):
      :
      :
        rows = []
        lastField = fields[len(fields)-1] # dlypka
        for item in items:
            new_item = []
            for t in fields:
                if t == 'id':
                    new_item.append(int(item.key().id()))
                else:
                    new_item.append(getattr(item, t))
            if lastField == 'gaeRef': # dlypka
                new_item[len(new_item)-1] = item  # dlypka
            rows.append(new_item)
        colnames = ['%s.%s' % (tablename, t) for t in fields]
#
---------------------------------------------------------------------------
-------------
# Proposal:
#  In place of this patch, I would prefer that web2py add a new field
type
#  named 'nativeRef', and gql.py method 'select' should store the
value of 'item' in such columns.
# With the 'nativeRef' type added to web2py, my proof of concept
would
be as follows:
      db.define_table('ParentTable',
                      db.Field('name','string', length=50), # NOTE!
for GAE you must
specify 'string', not 'text'!!
                      db.Field('gaeRef', 'nativeRef')
                    )
      db.define_table('ChildTable',
                     # NOTE: web2py provides the 'id' column
automatically
                      db.Field('idParentFolder','reference
ParentTable'),
                      db.Field('age','integer'),
                      db.Field('name','string', length=50), # NOTE!
for GAE you must
specify 'string', not 'text'!!
                      db.Field('parentLink',
gae.ReferenceProperty(db.ParentTable._tableobj, required=True,
collection_name='ChildCollection')),
                      db.Field('gaeRef', 'nativeRef')
                     )

    myParent = mydb.db.ParentTable.insert(nKey=1, name='Bill')

    myChild = mydb.db.ChildTable.insert(age = 9, name = 'Johnny',
                                   idParent = myParentid,
                                   parentLink =
myParent._table._last_reference)

    # Later, you can use web2py queries mixed with native GAE
Reference to find the children in a high performance single query:

    rows = mydb.db(mydb.db.ParentTable.name == 'Bill' ).select()
    myParentFromQuery = rows[0]

    for child in myParentFromQuery.gaeRef.ChildCollection:
        myChildName = child.name
        myChildAge = child.age
# ---- End of Proof of Concept
------------------------------------------------

Reply via email to