当select子句为零结果时,sqlalchemy insert-select from给出错误

时间:2016-01-13 15:55:15

标签: python postgresql sqlalchemy insert-select

下面是mwe(假设会话和引擎):

metadata=MetaData()
class node(Base):
    __tablename__ = 'nodes_tbl'
    pid = Column(Integer)
    __table_args__ = (
        PrimaryKeyConstraint('pid'),
        {},
        )

for i in range(0,100):
    session.add(node(i)) 

visited_tbl = Table('visited480', metadata, Column('visited', Integer, primary_key=True), Column('level', Integer, p\
rimary_key=False))

metadata.create_all(engine)

#this works                                                                                                          
session.execute(visited_tbl.insert().from_select(['visited', 'level'], session.query(node, sqlalchemy.sql.expression\
.literal_column('1').label('l')).filter(node.pid > 50)))


#this fails; possibly because of zero items                                                                          
session.execute(visited_tbl.insert().from_select(['visited', 'level'], session.query(node, sqlalchemy.sql.expression\
.literal_column('1').label('l')).filter(node.pid > 100)))

我得到的错误是    TypeError:'NoneType'对象没有属性__getitem__

令人困惑的是为什么Python出现在select和insert语句之间。它不应该创建一个SQL并让DB处理它吗?

0 个答案:

没有答案