python - Deleted rows from reflected table with SQLAlchemy -
i have table i'm trying delete data from. have been using session() object query data, , works fine. when go delete list of data, fails.
# load engine , 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 not. session.query(table).filter(table.c.column.in_([1,2,3,4,5])).delete()
when try delete bunch of rows, this:
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_'
i tried this question's method, gives me error:
file "/virtualenv/lib/python2.7/site-packages/sqlalchemy/sql/base.py", line 385, in execute raise exc.unboundexecutionerror(msg) sqlalchemy.exc.unboundexecutionerror: none not directly bound connection or engine.use .execute() method of connection or engine execute construct.
i tried map declarative base automap_base()
, got different errors.
how can delete rows table loaded in session have established?
the query interface part of sqlalchemy orm, , table
isn't mapped class.
the answer linked uses bound metadata (discouraged in modern sqlalchemy). following should work:
stmt = table.delete().where(table.c.column.in_([1,2,3,4,5])) engine.connect() conn: conn.execute(stmt)
edit:
i realised can this:
session.query(table).filter(table.c.column.in_([1,2,3,4,5])) \ .delete(synchronize_session=false)
Comments
Post a Comment