deleting records from table using SQLAlchemy

thclpr

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_'
thclpr

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deleting all table records from core data database using relationship

From Dev

Deleting multiple records in a table using join in Peewee?

From Dev

deleting records from a table where there is an inner join

From Dev

Deleting billions of records from Oracle Table containing trillions of records

From Dev

Deleting billions of records from Oracle Table containing trillions of records

From Dev

All records being deleted when deleting records from a temp table

From Dev

SQLite - no such table: while using CTE for deleting duplicate records

From Dev

Adding to a table using MVC 5, and reset Id after deleting records

From Dev

Finding duplicate records from table and deleting all but one with latest date

From Dev

Deleting checked records from table in asp.net

From Dev

Finding duplicate records from table and deleting all but one with latest date

From Dev

Preventing users from deleting records in one table that have related records in another table

From Dev

PDO deleting records from multiple tables using joins

From Dev

How to delete records from many-to-many (secondary) table in SQLAlchemy?

From Dev

Get users list from the table using SQLalchemy

From Dev

display records from a table using if statement

From Dev

Error from Entity Framework when deleting records from table with foreign key

From Dev

deleting a row from table

From Dev

Deleting Records in SQL server table prior to insert

From Dev

what precautions need to be taken before deleting records from a table in the production database

From Dev

INSERT statement conflicted with the FOREIGN KEY constraint even after deleting the records from the child table

From Dev

what precautions need to be taken before deleting records from a table in the production database

From Dev

Userform to delete specific records from table always duplicates the last record after deleting a specific record

From Dev

Deleting csv file vales from mysql table using single query

From Dev

Deleting from Oracle SQL table using 'inner join'

From Dev

Deleting specific row from a table in MySQL using PHP

From Dev

Get records from table and related related table using Linq to entity

From Dev

Insert records into one table using key from another table

From Dev

Update records of one table using data from another table

Related Related

  1. 1

    Deleting all table records from core data database using relationship

  2. 2

    Deleting multiple records in a table using join in Peewee?

  3. 3

    deleting records from a table where there is an inner join

  4. 4

    Deleting billions of records from Oracle Table containing trillions of records

  5. 5

    Deleting billions of records from Oracle Table containing trillions of records

  6. 6

    All records being deleted when deleting records from a temp table

  7. 7

    SQLite - no such table: while using CTE for deleting duplicate records

  8. 8

    Adding to a table using MVC 5, and reset Id after deleting records

  9. 9

    Finding duplicate records from table and deleting all but one with latest date

  10. 10

    Deleting checked records from table in asp.net

  11. 11

    Finding duplicate records from table and deleting all but one with latest date

  12. 12

    Preventing users from deleting records in one table that have related records in another table

  13. 13

    PDO deleting records from multiple tables using joins

  14. 14

    How to delete records from many-to-many (secondary) table in SQLAlchemy?

  15. 15

    Get users list from the table using SQLalchemy

  16. 16

    display records from a table using if statement

  17. 17

    Error from Entity Framework when deleting records from table with foreign key

  18. 18

    deleting a row from table

  19. 19

    Deleting Records in SQL server table prior to insert

  20. 20

    what precautions need to be taken before deleting records from a table in the production database

  21. 21

    INSERT statement conflicted with the FOREIGN KEY constraint even after deleting the records from the child table

  22. 22

    what precautions need to be taken before deleting records from a table in the production database

  23. 23

    Userform to delete specific records from table always duplicates the last record after deleting a specific record

  24. 24

    Deleting csv file vales from mysql table using single query

  25. 25

    Deleting from Oracle SQL table using 'inner join'

  26. 26

    Deleting specific row from a table in MySQL using PHP

  27. 27

    Get records from table and related related table using Linq to entity

  28. 28

    Insert records into one table using key from another table

  29. 29

    Update records of one table using data from another table

HotTag

Archive