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:
parent
93ba1a6398
commit
8427a77c65
2 changed files with 6 additions and 1 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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."""
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue