Do not add an association 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.
This commit is contained in:
Alexander Hess 2026-12-06 12:08:07 +01:00
commit 8427a77c65
Signed by: alexander
GPG key ID: B46EDB4DFC26805D
2 changed files with 6 additions and 1 deletions

View file

@ -123,7 +123,10 @@ class Grid(meta.Base):
pixel = pixels[(n_x, n_y)] 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) pixel.addresses.append(assoc)
return grid return grid

View file

@ -139,6 +139,7 @@ class TestGridification:
assert isinstance(result, db.Grid) assert isinstance(result, db.Grid)
assert len(result.pixels) == 1 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): def test_one_pixel_with_two_addresses(self, city, make_order, addresses_mock):
"""At the very least, there must be one `Pixel` ... """At the very least, there must be one `Pixel` ...
@ -159,6 +160,7 @@ class TestGridification:
assert isinstance(result, db.Grid) assert isinstance(result, db.Grid)
assert len(result.pixels) == 1 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): def test_no_pixel_with_one_address_too_far_south(self, city, order, addresses_mock):
"""An `address` outside the `city`'s viewport is discarded.""" """An `address` outside the `city`'s viewport is discarded."""