Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1090 fix(m2m): Return empty list if there are no m2m entries #1089

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions piccolo/columns/m2m.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,12 @@ async def run(self):
.output(as_list=True)
)

results = await secondary_table.objects().where(
secondary_table._meta.primary_key.is_in(ids)
results = (
await secondary_table.objects().where(
secondary_table._meta.primary_key.is_in(ids)
)
if len(ids) > 0
else []
)

return results
Expand Down
16 changes: 16 additions & 0 deletions tests/columns/m2m/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,22 @@ def test_get_m2m(self):

self.assertEqual([i.name for i in genres], ["Rock", "Folk"])

def test_get_m2m_no_rows(self):
"""
If there are no matching objects, then an empty list should be
returned.

https://github.com/piccolo-orm/piccolo/issues/1090

"""
band = Band.objects().get(Band.name == "Pythonistas").run_sync()
assert band is not None

Genre.delete(force=True).run_sync()

genres = band.get_m2m(Band.genres).run_sync()
self.assertEqual(genres, [])

def test_remove_m2m(self):
"""
Make sure we can remove related items via the joining table.
Expand Down
Loading