本文整理汇总了Python中sqlalchemy.test.schema.Table.outerjoin方法的典型用法代码示例。如果您正苦于以下问题:Python Table.outerjoin方法的具体用法?Python Table.outerjoin怎么用?Python Table.outerjoin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.test.schema.Table
的用法示例。
在下文中一共展示了Table.outerjoin方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: define_tables
# 需要导入模块: from sqlalchemy.test.schema import Table [as 别名]
# 或者: from sqlalchemy.test.schema.Table import outerjoin [as 别名]
def define_tables(cls, metadata):
global Table1, Table1B, Table2, Table3, Data
table1 = Table('table1', metadata,
Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
Column('related_id', Integer, ForeignKey('table1.id'), nullable=True),
Column('type', String(30)),
Column('name', String(30))
)
table2 = Table('table2', metadata,
Column('id', Integer, ForeignKey('table1.id'), primary_key=True),
)
table3 = Table('table3', metadata,
Column('id', Integer, ForeignKey('table1.id'), primary_key=True),
)
data = Table('data', metadata,
Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
Column('node_id', Integer, ForeignKey('table1.id')),
Column('data', String(30))
)
#join = polymorphic_union(
# {
# 'table3' : table1.join(table3),
# 'table2' : table1.join(table2),
# 'table1' : table1.select(table1.c.type.in_(['table1', 'table1b'])),
# }, None, 'pjoin')
join = table1.outerjoin(table2).outerjoin(table3).alias('pjoin')
#join = None
class Table1(object):
def __init__(self, name, data=None):
self.name = name
if data is not None:
self.data = data
def __repr__(self):
return "%s(%s, %s, %s)" % (self.__class__.__name__, self.id, repr(str(self.name)), repr(self.data))
class Table1B(Table1):
pass
class Table2(Table1):
pass
class Table3(Table1):
pass
class Data(object):
def __init__(self, data):
self.data = data
def __repr__(self):
return "%s(%s, %s)" % (self.__class__.__name__, self.id, repr(str(self.data)))
try:
# this is how the mapping used to work. ensure that this raises an error now
table1_mapper = mapper(Table1, table1,
select_table=join,
polymorphic_on=table1.c.type,
polymorphic_identity='table1',
properties={
'nxt': relation(Table1,
backref=backref('prev', foreignkey=join.c.id, uselist=False),
uselist=False, primaryjoin=join.c.id==join.c.related_id),
'data':relation(mapper(Data, data))
},
order_by=table1.c.id)
table1_mapper.compile()
assert False
except:
assert True
clear_mappers()
# currently, the "eager" relationships degrade to lazy relationships
# due to the polymorphic load.
# the "nxt" relation used to have a "lazy=False" on it, but the EagerLoader raises the "self-referential"
# exception now. since eager loading would never work for that relation anyway, its better that the user
# gets an exception instead of it silently not eager loading.
# NOTE: using "nxt" instead of "next" to avoid 2to3 turning it into __next__() for some reason.
table1_mapper = mapper(Table1, table1,
#select_table=join,
polymorphic_on=table1.c.type,
polymorphic_identity='table1',
properties={
'nxt': relation(Table1,
backref=backref('prev', remote_side=table1.c.id, uselist=False),
uselist=False, primaryjoin=table1.c.id==table1.c.related_id),
'data':relation(mapper(Data, data), lazy=False, order_by=data.c.id)
},
order_by=table1.c.id
)
table1b_mapper = mapper(Table1B, inherits=table1_mapper, polymorphic_identity='table1b')
table2_mapper = mapper(Table2, table2,
inherits=table1_mapper,
polymorphic_identity='table2')
#.........这里部分代码省略.........