<s> """<STR_LIT>""" <EOL> from datetime import datetime <EOL> from sqlalchemy import ( create_engine , MetaData , Table , Column , Integer , <EOL> String , DateTime , Float , ForeignKey , and_ ) <EOL> from sqlalchemy . orm import mapper , relationship , Session <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> from sqlalchemy . ext . associationproxy import association_proxy <EOL> Base = declarative_base ( ) <EOL> class Order ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> order_id = Column ( Integer , primary_key = True ) <EOL> customer_name = Column ( String ( <NUM_LIT:30> ) , nullable = False ) <EOL> order_date = Column ( DateTime , nullable = False , default = datetime . now ( ) ) <EOL> order_items = relationship ( "<STR_LIT>" , cascade = "<STR_LIT>" , <EOL> backref = '<STR_LIT>' ) <EOL> items = association_proxy ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __init__ ( self , customer_name ) : <EOL> self . customer_name = customer_name <EOL> class Item ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> item_id = Column ( Integer , primary_key = True ) <EOL> description = Column ( String ( <NUM_LIT:30> ) , nullable = False ) <EOL> price = Column ( Float , nullable = False ) <EOL> def __init__ ( self , description , price ) : <EOL> self . description = description <EOL> self . price = price <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . description , self . price <EOL> ) <EOL> class OrderItem ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> order_id = Column ( Integer , ForeignKey ( '<STR_LIT>' ) , primary_key = True ) <EOL> item_id = Column ( Integer , ForeignKey ( '<STR_LIT>' ) , primary_key = True ) <EOL> price = Column ( Float , nullable = False ) <EOL> def __init__ ( self , item , price = None ) : <EOL> self . item = item <EOL> self . price = price or item . price <EOL> item = relationship ( Item , lazy = '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> engine = create_engine ( '<STR_LIT>' ) <EOL> Base . metadata . create_all ( engine ) <EOL> session = Session ( engine ) <EOL> tshirt , mug , hat , crowbar = ( <EOL> Item ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> Item ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> Item ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> Item ( '<STR_LIT>' , <NUM_LIT> ) <EOL> ) <EOL> session . add_all ( [ tshirt , mug , hat , crowbar ] ) <EOL> session . commit ( ) <EOL> order = Order ( '<STR_LIT>' ) <EOL> order . items . append ( mug ) <EOL> order . items . append ( hat ) <EOL> order . order_items . append ( OrderItem ( crowbar , <NUM_LIT> ) ) <EOL> session . add ( order ) <EOL> session . commit ( ) <EOL> order = session . query ( Order ) . filter_by ( customer_name = '<STR_LIT>' ) . one ( ) <EOL> print ( [ ( assoc . item . description , assoc . price , assoc . item . price ) <EOL> for assoc in order . order_items ] ) <EOL> print ( [ ( item . description , item . price ) <EOL> for item in order . items ] ) <EOL> orders = session . query ( Order ) . join ( '<STR_LIT>' , '<STR_LIT>' ) . filter ( Item . description == '<STR_LIT>' ) . filter ( Item . price > OrderItem . price ) <EOL> print ( [ o . customer_name for o in orders ] ) </s>
