Left join - Get all Managers with their Bands if they exist #208
Unanswered
davidolrik
asked this question in
Q&A
Replies: 2 comments 1 reply
-
There's isn't an API for this currently. The best workaround I can think of is to do two separate queries, and then splice them together using Python: managers = Manager.select().run_sync()
bands = Band.select(Band.name, Band.manager.id).order_by(Band.manager.id).run_sync()
band_map = {}
for key, group in itertools.groupby(
bands,
lambda x: x['manager.id']
):
band_map[key] = list(group)
for manager in managers:
manager['bands'] = band_map.get(manager['id'], []) I can convert this discussion into an issue, so a proper API can be implemented. I'm not sure what the perfect API would be at the moment. Something like this? >>> await Manager.select(Manager.name, Manager.band_set(Band.name)).run()
[{'name': 'Guido', 'bands': [{'name': 'Pythonistas'}]}] |
Beta Was this translation helpful? Give feedback.
1 reply
-
Let's add managers = await Manager.select(Manager.name, Band.name).left_join(Band).on(Manager.id == Band.manager.id).run() It's clean, powerful and consistent with the framework idea. It's obvious syntax which doesn't have any magic. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is it possible to get all Manager objects with their Bands in one go? - Even if some managers don't have Bands.
Beta Was this translation helpful? Give feedback.
All reactions