Link new objects from the parent
Since SQLAlchemy 2.0, only the parent's end cascades a new object into the session'
This commit is contained in:
parent
87aaebe475
commit
ee5a372b2b
2 changed files with 21 additions and 6 deletions
|
|
@ -74,7 +74,9 @@ class Grid(meta.Base):
|
|||
Returns:
|
||||
grid: including `grid.pixels` with the associated `city.addresses`
|
||||
"""
|
||||
grid = cls(city=city, side_length=side_length)
|
||||
grid = cls(side_length=side_length)
|
||||
|
||||
city.grids.append(grid)
|
||||
|
||||
# `Pixel`s grouped by `.n_x`-`.n_y` coordinates.
|
||||
pixels = {}
|
||||
|
|
@ -102,7 +104,9 @@ class Grid(meta.Base):
|
|||
n_x, n_y = address.x // side_length, address.y // side_length
|
||||
# ... and create a new `Pixel` object if necessary.
|
||||
if (n_x, n_y) not in pixels:
|
||||
pixels[(n_x, n_y)] = db.Pixel(grid=grid, n_x=n_x, n_y=n_y)
|
||||
pixels[(n_x, n_y)] = db.Pixel(n_x=n_x, n_y=n_y)
|
||||
grid.pixels.append(pixels[(n_x, n_y)])
|
||||
|
||||
pixel = pixels[(n_x, n_y)]
|
||||
|
||||
# Create an association between the `address` and `pixel`.
|
||||
|
|
|
|||
|
|
@ -13,7 +13,11 @@ def make_address(city):
|
|||
|
||||
def func(**kwargs):
|
||||
"""Create an `Address` object in the `city`."""
|
||||
return factories.AddressFactory.build(city=city, **kwargs)
|
||||
address = factories.AddressFactory.build(**kwargs)
|
||||
|
||||
city.addresses.append(address)
|
||||
|
||||
return address
|
||||
|
||||
return func
|
||||
|
||||
|
|
@ -58,7 +62,11 @@ def make_restaurant(make_address):
|
|||
if address is None:
|
||||
address = make_address()
|
||||
|
||||
return factories.RestaurantFactory.build(address=address, **kwargs)
|
||||
restaurant = factories.RestaurantFactory.build(**kwargs)
|
||||
|
||||
address.restaurants.append(restaurant)
|
||||
|
||||
return restaurant
|
||||
|
||||
return func
|
||||
|
||||
|
|
@ -93,15 +101,18 @@ def make_order(make_address, make_courier, make_customer, make_restaurant):
|
|||
if courier is None:
|
||||
courier = make_courier()
|
||||
|
||||
return factory_cls.build(
|
||||
order = factory_cls.build(
|
||||
customer=make_customer(), # assume a unique `Customer` per order
|
||||
restaurant=restaurant,
|
||||
courier=courier,
|
||||
pickup_address=restaurant.address, # no `Address` history
|
||||
delivery_address=make_address(), # unique `Customer` => new `Address`
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
restaurant.orders.append(order)
|
||||
|
||||
return order
|
||||
|
||||
return func
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue