views:

198

answers:

1

Hi all,

I don't know how to make a GqlQuery that order by a property of the ReferenceProperty.

In this case, I want to get all the Seat but order by the id of the Room

The GqlQuery reference does not allow a join, so how do you approach in this case?

class Room(db.Model):
    id = db.IntegerProperty()
    dimension = db.StringProperty(multiline=True)

a room has many seats, each with an id

class Seat(db.Model):
    id = db.IntegerProperty()
    roomId = db.ReferenceProperty(Room)



seats_query = GqlQuery("SELECT * FROM Seat ")
seats = seats_query.fetch(1000)

Alex is right on his point, so I decided not to touch GqlQuery, and tried to play with Django template instead, I have found the solution to sort Seat by Room'id without having to add new field in Seat class.

I put the code here if anyone has interests ;)) Cheers ;))

  {% regroup seats|dictsort:"roomId.id" by roomId.id as room_list %}
  <ul>
   {% for room in room_list %}
    <li>Room: {{room.grouper}}
      <ul>
   {% for item in room.list %}
   <li>Seat no: {{item.id}} </li>
   {% endfor %}
      </ul>
    </li>
    {% endfor %}
  </ul>
A: 

In general, to work around non-relational DBs lack of JOIN functionality, you denormalize; specifically, you redundantly put data pieces in more than one place, so they can be effectively accessed in your queries (this does make it more cumbersome to update your store, but then, non-relational DBs in general are heavily slanted to read-mostly applications).

In this case, you add to Seat a property with the room's id -- since you have peculiarly called roomId (rather than, say, room) the reference to the seat's room, I guess you'd have this denormalized bit as roomIdId.

Alex Martelli
omg, this means that I don't have any solution to order the Seat by id of the Room without adding extra data to my models :-sWhat to do if you already have tons of data in your database?
sfa
@Hoang, you can add a field to all existing Seats, N at a time, for example one roomful of seats per bunch should be fine (use task queues). In general you can only order by fields you have -- that's a key limitation of nonrelational DBs (in exchange for scalability etc).
Alex Martelli
hi Alex, I modified the question with answer using Django template to solve my issue ;) Cheers ;)
sfa
If you want to sort in application-level code you can do it in many ways, of course (doing it in the template is a bit extreme IMHO, a good compromise might be to do it in the view's Python code); I thought you wanted the store to do it for you.
Alex Martelli