From 8427a77c656e4b9bd278a06bdf69f0fbb4d249fa Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Sun, 6 Dec 2026 12:08:07 +0100 Subject: [PATCH] Do not add an `assoc`iation to its `Pixel` twice `AddressPixelAssociation(..., pixel=pixel)` already adds the `assoc`iation to `pixel.addresses` via `back_populates`. The explicit `.append()` afterwards put the same object in the list again! Yet, only the `.append()` cascades the `assoc`iation into the `session` because `sqlalchemy` v2.x no longer cascades along `backref`s. => Keep the `.append()` and drop the `pixel=pixel` The database was not affected as the flush inserts an object only once. --- src/velofy/db/grids.py | 5 ++++- tests/db/test_grids.py | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/velofy/db/grids.py b/src/velofy/db/grids.py index f23c460..9b84c2b 100644 --- a/src/velofy/db/grids.py +++ b/src/velofy/db/grids.py @@ -123,7 +123,10 @@ class Grid(meta.Base): pixel = pixels[(n_x, n_y)] - assoc = db.AddressPixelAssociation(address=address, pixel=pixel) + # Only appending to `.addresses` + # cascades the `assoc`iation into the `session` + # => Passing `pixel=pixel` as well would add it to `.addresses` twice + assoc = db.AddressPixelAssociation(address=address) pixel.addresses.append(assoc) return grid diff --git a/tests/db/test_grids.py b/tests/db/test_grids.py index b074705..1f1f975 100644 --- a/tests/db/test_grids.py +++ b/tests/db/test_grids.py @@ -139,6 +139,7 @@ class TestGridification: assert isinstance(result, db.Grid) assert len(result.pixels) == 1 + assert len(result.pixels[0].addresses) == 1 def test_one_pixel_with_two_addresses(self, city, make_order, addresses_mock): """At the very least, there must be one `Pixel` ... @@ -159,6 +160,7 @@ class TestGridification: assert isinstance(result, db.Grid) assert len(result.pixels) == 1 + assert len(result.pixels[0].addresses) == 2 def test_no_pixel_with_one_address_too_far_south(self, city, order, addresses_mock): """An `address` outside the `city`'s viewport is discarded."""