<s> """<STR_LIT>""" <EOL> from sqlalchemy import Table , Column , Integer , String , ForeignKey , create_engine , inspect , or_ <EOL> from sqlalchemy . orm import relationship , Session , with_polymorphic <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> Base = declarative_base ( ) <EOL> class Company ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> name = Column ( String ( <NUM_LIT:50> ) ) <EOL> employees = relationship ( "<STR_LIT>" , <EOL> backref = '<STR_LIT>' , <EOL> cascade = '<STR_LIT>' ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> class Person ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> company_id = Column ( Integer , ForeignKey ( '<STR_LIT>' ) ) <EOL> name = Column ( String ( <NUM_LIT:50> ) ) <EOL> type = Column ( String ( <NUM_LIT:50> ) ) <EOL> __mapper_args__ = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : type <EOL> } <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> class Engineer ( Person ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = Column ( Integer , ForeignKey ( '<STR_LIT>' ) , primary_key = True ) <EOL> status = Column ( String ( <NUM_LIT:30> ) ) <EOL> engineer_name = Column ( String ( <NUM_LIT:30> ) ) <EOL> primary_language = Column ( String ( <NUM_LIT:30> ) ) <EOL> __mapper_args__ = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" "<STR_LIT>" % ( self . name , self . status , <EOL> self . engineer_name , self . primary_language ) <EOL> class Manager ( Person ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = Column ( Integer , ForeignKey ( '<STR_LIT>' ) , primary_key = True ) <EOL> status = Column ( String ( <NUM_LIT:30> ) ) <EOL> manager_name = Column ( String ( <NUM_LIT:30> ) ) <EOL> __mapper_args__ = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , self . status , self . manager_name ) <EOL> engine = create_engine ( '<STR_LIT>' , echo = True ) <EOL> Base . metadata . create_all ( engine ) <EOL> session = Session ( engine ) <EOL> c = Company ( name = '<STR_LIT>' , employees = [ <EOL> Manager ( <EOL> name = '<STR_LIT>' , <EOL> status = '<STR_LIT>' , <EOL> manager_name = '<STR_LIT>' ) , <EOL> Engineer ( name = '<STR_LIT>' , <EOL> status = '<STR_LIT>' , <EOL> engineer_name = '<STR_LIT>' , <EOL> primary_language = '<STR_LIT>' ) , <EOL> Person ( name = '<STR_LIT>' ) , <EOL> Engineer ( name = '<STR_LIT>' , <EOL> status = '<STR_LIT>' , <EOL> engineer_name = '<STR_LIT>' , <EOL> primary_language = '<STR_LIT>' ) , <EOL> Manager ( name = '<STR_LIT>' , <EOL> status = '<STR_LIT>' , <EOL> manager_name = '<STR_LIT>' ) <EOL> ] ) <EOL> session . add ( c ) <EOL> session . commit ( ) <EOL> c = session . query ( Company ) . get ( <NUM_LIT:1> ) <EOL> for e in c . employees : <EOL> print ( e , inspect ( e ) . key , e . company ) <EOL> assert set ( [ e . name for e in c . employees ] ) == set ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> print ( "<STR_LIT:\n>" ) <EOL> dilbert = session . query ( Person ) . filter_by ( name = '<STR_LIT>' ) . one ( ) <EOL> dilbert2 = session . query ( Engineer ) . filter_by ( name = '<STR_LIT>' ) . one ( ) <EOL> assert dilbert is dilbert2 <EOL> dilbert . engineer_name = '<STR_LIT>' <EOL> session . commit ( ) <EOL> c = session . query ( Company ) . get ( <NUM_LIT:1> ) <EOL> for e in c . employees : <EOL> print ( e ) <EOL> eng_manager = with_polymorphic ( Person , [ Engineer , Manager ] , aliased = True ) <EOL> print ( session . query ( eng_manager ) . filter ( <EOL> or_ ( eng_manager . Engineer . engineer_name == '<STR_LIT>' , <EOL> eng_manager . Manager . manager_name == '<STR_LIT>' <EOL> ) <EOL> ) . all ( ) ) <EOL> eng_manager = with_polymorphic ( Person , [ Engineer , Manager ] , aliased = True ) <EOL> print ( session . query ( Company ) . join ( <EOL> eng_manager , <EOL> Company . employees <EOL> ) . filter ( <EOL> or_ ( eng_manager . Engineer . engineer_name == '<STR_LIT>' , <EOL> eng_manager . Manager . manager_name == '<STR_LIT>' ) <EOL> ) . all ( ) ) <EOL> session . commit ( ) </s>
