0

I want to erase some data from a test table. But even after following this documentation, I'm not sure what I'm doing wrong.:

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()

error message:

 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_'
2
  • Very much related: stackoverflow.com/questions/9882358/… Commented Feb 13, 2017 at 14:00
  • @IljaEverilä I tried this solution, but it didn't worked., it returned: 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. Commented Feb 13, 2017 at 15:53

1 Answer 1

0

Solution found on this post

I had to use:

d = t_packages.delete(t_packages.c.environment == 'sandbox')
with engine.connect() as conn:
    conn.execute(d)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.