coleifer/peewee — pull request #471 by arrowgamer (2014-12-01 11:04:37+00:00)
Title: Fixed issue with aggregate_rows

Hi there.

Awesome job on this ORM!

I ran into an issue while using it last night, however, and here is my proposed solution. As not the most seasoned Python developer, your code is quite difficult for me to understand, so I don't know if this implemented correctly or if the change is even warranted. Perhaps I was using Peewee incorrectly.

My issue was as follows. I have ModelA with two foreign keys to both to ModelB.

**Example:**
```python
class ModelB(Model):
manager = ForeignKeyField(ModelA, related_name='slaves')
assistant = ForeignKeyField(ModelA, related_name='masters')
```

Using aggregate_rows to preload an instance of ModelA with its masters would place the results under the wrong related_field, slaves, for instance. I thought this might be because Peewee was handling this case incorrectly, and from what I understand about the code, the "rel_for_model" function accounts for this if you pass the field_obj argument, though that's not being passed through the calls to "reverse_rel_for_model", and so it's not really caring about the exact foreign key field and thus populating the wrong "related_name" field.

Does that make any sense?