Skip to content
Snippets Groups Projects
Commit e9595857 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

added many-to-many with extra payload

parent d4f05a13
Branches extra-payload
No related tags found
No related merge requests found
...@@ -26,21 +26,26 @@ class Item(Persisted): ...@@ -26,21 +26,26 @@ class Item(Persisted):
name = Column(String(256), nullable=False) name = Column(String(256), nullable=False)
price = Column(Integer, nullable=False) price = Column(Integer, nullable=False)
menu = relationship('Menu', back_populates='items') menu = relationship('Menu', back_populates='items')
orders = relationship('Order', uselist=True, secondary='order_items', back_populates='items') orders = relationship('Order', uselist=True, secondary='order_items', viewonly=True)
# # # If there isn't any extra payload in the join table, there's no need to explicitly mention it order_items = relationship('OrderItem', uselist=True, back_populates='item')
# # # If there is extra payload in the join table, then we need to make sure Item & Order can access it
class Order(Persisted): class Order(Persisted):
__tablename__ = 'orders' __tablename__ = 'orders'
order_id = Column(Integer, primary_key=True) order_id = Column(Integer, primary_key=True)
timestamp = Column(DateTime) timestamp = Column(DateTime)
items = relationship('Item', uselist=True, secondary='order_items', back_populates='orders') items = relationship('Item', uselist=True, secondary='order_items', viewonly=True)
order_items = relationship('OrderItem', uselist=True, back_populates='order')
class OrderItem(Persisted): class OrderItem(Persisted):
__tablename__ = 'order_items' __tablename__ = 'order_items'
order_id = Column(Integer, ForeignKey('orders.order_id', ondelete='CASCADE'), primary_key=True) order_id = Column(Integer, ForeignKey('orders.order_id', ondelete='CASCADE'), primary_key=True)
item_id = Column(Integer, ForeignKey('items.item_id', ondelete='CASCADE'), primary_key=True) item_id = Column(Integer, ForeignKey('items.item_id', ondelete='CASCADE'), primary_key=True)
amount = Column(Integer)
item = relationship('Item', back_populates='order_items')
order = relationship('Order', back_populates='order_items')
class DinerDatabase(object): class DinerDatabase(object):
......
...@@ -15,7 +15,7 @@ def add_starter_data(session): ...@@ -15,7 +15,7 @@ def add_starter_data(session):
orange_juice = Item(menu=beverage_menu, name='Orange Juice', price=149) orange_juice = Item(menu=beverage_menu, name='Orange Juice', price=149)
short_stack = Item(menu=food_menu, name='Pancakes', price=399) short_stack = Item(menu=food_menu, name='Pancakes', price=399)
regular_pancakes = Item(menu=food_menu, name='Pancakes', price=499) regular_pancakes = Item(menu=food_menu, name='Pancakes', price=499)
first_order = Order(items=[orange_juice]) first_order = Order(order_items=[OrderItem(item=orange_juice, amount=2)])
session.add(beverage_menu) session.add(beverage_menu)
session.add(food_menu) session.add(food_menu)
session.add(orange_juice) session.add(orange_juice)
...@@ -41,6 +41,8 @@ def main(): ...@@ -41,6 +41,8 @@ def main():
print([item.name for item in the_order.items]) print([item.name for item in the_order.items])
orange_juice: Item = session.query(Item).filter(Item.name == 'Orange Juice').one() orange_juice: Item = session.query(Item).filter(Item.name == 'Orange Juice').one()
print([order.order_id for order in orange_juice.orders]) print([order.order_id for order in orange_juice.orders])
print([f'{order_item.amount} {order_item.item.name}s' for order_item in the_order.order_items])
print([f'{order_item.amount} {orange_juice.name}s on order #{order_item.order.order_id}' for order_item in orange_juice.order_items])
except SQLAlchemyError as exception: except SQLAlchemyError as exception:
print('Database setup failed!', file=stderr) print('Database setup failed!', file=stderr)
print(f'Cause: {exception}', file=stderr) print(f'Cause: {exception}', file=stderr)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment