Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Alchemy - How to Delete from a model instance?

Tags:

Say I get a model instance like this:

instance = session.query(MyModel).filter_by(id=1).first() 

How can I delete that row? Is there a special method to call?

like image 460
Greg Avatar asked Jul 23 '10 21:07

Greg


People also ask

How do I delete an item in SQLAlchemy?

The delete() SQL Expression Construct The delete() function generates a new instance of Delete which represents a DELETE statement in SQL, that will delete rows from a table.

How do I delete a record in SQLAlchemy ORM?

Delete multiple rows in SQLAlchemyGet the books table from the Metadata object initialized while connecting to the database. Pass the delete query to the execute() function and get all the results using fetchall() function.

How do I update data in SQLAlchemy?

Update table elements in SQLAlchemy. Get the books to table from the Metadata object initialized while connecting to the database. Pass the update query to the execute() function and get all the results using fetchall() function. Use a for loop to iterate through the results.


1 Answers

Ok I found it after further searching:

session.delete(instance) 
like image 142
Greg Avatar answered Sep 18 '22 13:09

Greg