Commit b61a126c authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Added relationships and uncommented code to create starter data.

parent 4daf672f
......@@ -15,6 +15,7 @@ class Menu(Persisted):
__tablename__ = 'menus'
menu_id = Column(Integer, primary_key=True) # primary keys default to auto_increment
name = Column(String(256), nullable=False) # nullable is the default
items = relationship('Item', uselist=True, back_populates='menu')
class Item(Persisted):
......@@ -24,12 +25,17 @@ class Item(Persisted):
menu_id = Column(Integer, ForeignKey('menus.menu_id', ondelete='CASCADE'), nullable=False)
name = Column(String(256), nullable=False)
price = Column(Integer, nullable=False)
menu = relationship('Menu', back_populates='items')
order_items = relationship('OrderItem', uselist=True, back_populates='item')
orders = relationship('Order', uselist=True, secondary='order_items')
class Order(Persisted):
__tablename__ = 'orders'
order_id = Column(Integer, primary_key=True)
timestamp = Column(DateTime)
order_items = relationship('OrderItem', uselist=True, back_populates='order')
items = relationship('Item', uselist=True, secondary='order_items')
class OrderItem(Persisted):
......@@ -37,6 +43,8 @@ class OrderItem(Persisted):
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)
amount = Column(Integer)
item = relationship('Item', back_populates='order_items')
order = relationship('Order', back_populates='order_items')
class DinerDatabase(object):
......
......@@ -30,13 +30,13 @@ def main():
diner_database = DinerDatabase(url)
diner_database.ensure_tables_exist()
print('Tables created.')
# session = diner_database.create_session()
# if already_has_data(session):
# print('Not creating records because some already exist.')
# else:
# add_starter_data(session)
# session.commit()
# print('Records created.')
session = diner_database.create_session()
if already_has_data(session):
print('Not creating records because some already exist.')
else:
add_starter_data(session)
session.commit()
print('Records created.')
except SQLAlchemyError as exception:
print('Database setup failed!', file=stderr)
print(f'Cause: {exception}', file=stderr)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment