Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to order by properties in the reference property, any good solution?

Say I have 2 kind:

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

  create_time = db.DataTimeProperty()
  last_login = db.DateTimeProperty()
  last_update = db.DataTimeProperty()


class Relationship(db.Model)
  owner = db.ReferenceProperty(Account)
  target = db.ReferenceProperty(Account)
  type = db.IntegerProperty()

I want to get the equivalence of following query:

SELECT target 
FROM Relationship
WHERE owner = :key AND type = :type
ORDERBY target.last_login DESC 

How to do that?

reference: http://www.mail-archive.com/[email protected]/msg15878.html

like image 778
Robert Mao Avatar asked Dec 19 '25 08:12

Robert Mao


1 Answers

There's no equivalent for that query in datastore. Some points:

  1. You can't select a single property. SELECT is always SELECT * (you select a whole entity).
  2. You can't do joins. You need to denormalize your models to fit the queries you will perform, or perform multiple queries.

So to achieve your goal, you need to have last_login stored in Relationship, or have a 3rd model to serve as index for that specific query.

like image 197
moraes Avatar answered Dec 21 '25 03:12

moraes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!