我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用sqlalchemy.orm.relation()。
def orm_load_flucstrucs(man): man.flucstruc_table = Table('flucstrucs', man.metadata, Column('basedata_id', Integer, ForeignKey('basedata.basedata_id'), primary_key=True), Column('_binary_svs', Integer), Column('freq', Float), Column('a12', Float), Column('t0', Float), Column('p', Float), Column('H', Float), Column('dphase_id', Integer, ForeignKey('baseordereddataset.id'), nullable=False)) man.metadata.create_all() mapper(FlucStruc, man.flucstruc_table, inherits=BaseData, polymorphic_identity='flucstruc', properties={'dphase': relation(BaseOrderedDataSet)})
def orm_load_channel(man): man.channel_table = Table('channel', man.metadata, Column('id', Integer, primary_key=True), Column('name', String(200), nullable=False), Column('coords_id', Integer, ForeignKey('coords.id'), nullable=False)) #man.metadata.create_all() mapper(Channel, man.channel_table, properties={'coords': relation(Coords)})
def orm_load_channellist(man): man.channellist_table = Table('channellist', man.metadata, Column('id', Integer, primary_key=True)) #man.metadata.create_all() mapper(ChannelList, man.channellist_table, properties={'_channels': relation(Channel, secondary=man.channel_association_table)})
def orm_load_dataset(man): man.dataset_table = Table('dataset', man.metadata, Column('basedataset_id', Integer, ForeignKey('basedataset.id'), primary_key=True)) #man.metadata.create_all() mapper(DataSet, man.dataset_table, inherits=BaseDataSet, polymorphic_identity='dataset', properties={'data': relation(BaseData, secondary=man.data_basedataset_table, backref='datasets', cascade='all', collection_class=set)})
def orm_load_baseordereddataset(man): man.baseordereddataset_table = Table('baseordereddataset', man.metadata, Column('id', Integer, primary_key=True), Column('created', DateTime), Column('label', String(50), nullable=False, unique=True), Column('type', String(30), nullable=False)) man.ordereditems_table = Table('ordereddata_items', man.metadata, Column('dataset_id', Integer, ForeignKey('baseordereddataset.id'), primary_key=True), Column('item_id', Integer, ForeignKey('basedata.basedata_id'), primary_key=True), Column('index', Integer, nullable=False) ) man.metadata.create_all() mapper(BaseOrderedDataSet, man.baseordereddataset_table, polymorphic_on=man.baseordereddataset_table.c.type, polymorphic_identity='base_ordered_dataset', properties={'data_items': relation(OrderedDataSetItem, backref='ordered_datasets_items', cascade='all, delete-orphan', collection_class=column_mapped_collection(man.ordereditems_table.c.index)) } ) mapper(OrderedDataSetItem, man.ordereditems_table, properties={ 'item': relation(BaseData, lazy='joined', backref='dataitem') })