Open lottspot opened 6 years ago
This issue blocks progress on #39
Also, is this actually a bug, or am I just doing this wrong/expecting the wrong things?
Hopped into a shell and played with the code directly on the python CLI, which turned in some interesting results. First, I retrieved the query object similarly to how the /candidates endpoint does so.
>>> from leverageapi import models
>>> s = models.session
>>> rid = 28
>>> m1 = models.Candidate
>>> m2 = models.Candidacy
>>> q = s.query(m1).join(m1.candidacies).filter(m2.race_id==rid)
Then I checked on whether the candidacies
relationship is actually loading, as is indicated that it should in the models definition.
>>> o = list(q)
>>> o[0].candidacies
[<Candidacy 'Rebecca Rhynhart' '', (2017 'general')>]
>>> o[0].candidacies[0].party.party_name
'Democratic'
So the relationship is there after the query is completed; it just isn't being passed through to the object that the endpoint actually returns. This makes me believe that this issue is being caused by the implementation of the Candidacy.as_dict
method, which is responsible for creating the object representation returned to API clients.
Interesting that the string 'candidacies' fails the test it would need to be included in the object returned to the API client
>>> 'candidacies' in o[0].__table__.columns
False
The problem of serializing nested sqlalchemy objects to JSON is not trivial as it turns out. Potential solutions I've researched so far include:
Also, it seems like maybe sqlalchemy collections offer a route to get there? If they are a possible route, it seems like the most complex one.
I was able to basically understand the JSON encoder solution in a single reading of it, so I think it's probably the first solution I'll attempt to implement.
Example request:
Based on the logic which performs retrieval from the database and the definition of the Candidacy object itself I would expect the "party" field to be present on each object in the "candidacies" array. In the sample request however, it is not.