Skip to content

Instantly share code, notes, and snippets.

@bharris62
Created September 22, 2016 01:32
Show Gist options
  • Save bharris62/29bb51604b1ee9cbc28456410d2ebb27 to your computer and use it in GitHub Desktop.
Save bharris62/29bb51604b1ee9cbc28456410d2ebb27 to your computer and use it in GitHub Desktop.
from sqlalchemy import Table, Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
pizza_topping_table = Table('pizza_topping_association', Base.metadata,
Column('pizza_id', Integer, ForeignKey('pizza.id')),
Column('topping_id', Integer, ForeignKey('topping.id'))
)
class Pizza(Base):
__tablename__ = 'pizza'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
toppings = relationship("Topping", secondary="pizza_topping_association",
backref="pizzas")
class Topping(Base):
__tablename__ = 'topping'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
peppers = Topping(name="Peppers")
garlic = Topping(name="Garlic")
chilli = Topping(name="Chilli")
spicy_pepper = Pizza(name="Spicy Pepper")
spicy_pepper.toppings = [peppers, chilli]
vampire_weekend = Pizza(name="Vampire Weekend")
vampire_weekend.toppings = [garlic, chilli]
session.add_all([garlic, peppers, chilli, spicy_pepper, vampire_weekend])
session.commit()
for topping in vampire_weekend.toppings:
print(topping.name)
for pizza in chilli.pizzas:
print(pizza.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment