【发布时间】:2016-06-11 01:40:03
【问题描述】:
我有一个表,我正试图从中删除数据。我一直在使用 Session() 对象来查询数据,它工作得很好。但是当我去删除一个数据列表时,它失败了。
# load engine and reflect.
engine = create_engine("...")
metadata = MetaData()
Session = sessionmaker(autoflush=True, autocommit=True)
Session.configure(bind=engine)
session = Session()
metadata.reflect(bind=engine)
# queries work.
table = Table("some_table", metadata, autoload_with=engine)
session.query(table).filter(table.c.column.between(dobj1,dobj2)).all()
# deletes do not.
session.query(table).filter(table.c.column.in_([1,2,3,4,5])).delete()
当我尝试删除一堆行时,我得到了这个:
File "/virtualenv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 1180, in _do_pre_synchronize
target_cls = query._mapper_zero().class_
AttributeError: 'Table' object has no attribute 'class_'
我尝试了this question's 方法,但它给了我这个错误:
File "/virtualenv/lib/python2.7/site-packages/sqlalchemy/sql/base.py", line 385, in execute
raise exc.UnboundExecutionError(msg)
sqlalchemy.exc.UnboundExecutionError: This None is not directly bound to a Connection or Engine.Use the .execute() method of a Connection or Engine to execute this construct.
我尝试使用 automap_base() 将其映射到声明性基础,但我遇到了不同的错误。
如何从我已经建立的会话中加载的表中删除行?
【问题讨论】:
标签: python postgresql sqlalchemy