0
我想從測試表中刪除一些數據。但是,即使下面this documentation後,我不知道我做錯了什麼:使用SQLAlchemy從表中刪除記錄
hostname = 'localhost'
db_name = 'xx'
db_port = 'xx'
login = 'xx'
pwd = 'xx'
con_string = 'mysql+mysqlconnector://{login}:{passwd}@{hostname}:{port}/{db}'
engine_str = con_string.format(
login=login, passwd=pwd, hostname=hostname, port=db_port, db=db_name
)
try:
engine = sqlalchemy.create_engine(engine_str, echo=False)
session = sessionmaker(bind=engine)
connection = engine.connect()
session = session(bind=connection)
Base = declarative_base()
except exc.SQLAlchemyError:
raise
t_packages = Table('test_table', Base.metadata, autoload_with=engine)
session.query(t_packages).filter_by(environment='sandbox').delete()
錯誤消息:
line 119, in delete_package_reference_by_env
session.query(t_packages).filter_by(environment=environment).delete()
File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line 3155, in delete
delete_op.exec_()
File "C:\Python27\lib\site-packages\sqlalchemy\orm\persistence.py", line 1167, in exec_
self._do_pre_synchronize()
File "C:\Python27\lib\site-packages\sqlalchemy\orm\persistence.py", line 1221, in _do_pre_synchronize
target_cls = query._mapper_zero().class_
AttributeError: 'NoneType' object has no attribute 'class_'
有很大關係:http://stackoverflow.com/questions/9882358/how-to-delete-rows-from-a-table-using-an-sqlalchemy-q uery-沒有-ORM –
@IljaEverilä我試過這個解決方案,但它並沒有奏效,它返回:sqlalchemy.exc.UnboundExecutionError:這一切都不是不直接鍵合連接或Engine.Use的.execute()的方法一個連接或引擎來執行這個構造。 –