I have two models in my application, Transaction and Person, with a many-to-many relationship. Each Transaction includes individuals. For each person, there is also an amount associated with each transaction with which the person is associated. Therefore, I need to model the many-to-many relationship with the relationship data. Google offers this approach:
http://code.google.com/intl/sv-SE/appengine/articles/modeling.html
With this approach, I have this model:
class TransactionPerson(db.Model):
But I believe that this is very bad for performance, because if I need to sum the sum for each Person in all transactions, I need to run the Person * Transaction * TransactionPerson cycle to implement the "merge" when summing the sums.
MY IDEA
My idea is to have two lists in the transaction model:
class Transaction(db.Model): persons = ListProperty(db.Key) persons_amount = ListProperty(float)
This way, I donβt need to iterate over all TransactionPerson transactions for each Person to find the related transaction. And I can still request transactions based on Person.
QUESTIONS
- Is it possible? Can you trust that the order of the list is always the same when saving / restoring, so that the indexes synchronize between the lists?
- Is this a good way to implement many-to-many relationships with related data?
source share