question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use delete operation with async orm?

See original GitHub issue

We used to use synchronous ORM operations using code below:

obj = session.query(User).get(id)
session.delete(obj)
session.commit()

How should we use delete operation in asynchronous ORM?

Versions:

  • Python: 3.8.5
  • SQLAlchemy: 1.4

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
VladislavRusakovcommented, Nov 18, 2021

I know it’s outdated, but I’ve met same problem and figured out, how to fix it.

@app.get("/db/del/{index}", response_model=list[Weather]) async def delete_row(index: int, session: AsyncSession = Depends(get_session)):

Inside function: row = await session.execute(select(Weather).where(Weather.id == index)) row = row.scalar_one() await session.delete(row) await session.commit()

Idk if it is right, but it works, at least for me.

4reactions
CaselITcommented, Oct 4, 2020

Hi,

The documentation is probably the best place to start: https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html

Read more comments on GitHub >

github_iconTop Results From Across the Web

Delete | SeaORM An async & dynamic ORM for Rust - SeaQL
Find a Model from the database, then delete the corresponding row from database. use sea_orm::entity::ModelTrait;
Read more >
Asynchronous I/O (asyncio) - SQLAlchemy 1.4 Documentation
It's advisable to invoke the AsyncEngine.dispose() method using await when using the AsyncEngine object in a scope that will go out of context ......
Read more >
python + SQLAlchemy: deleting with the Session object
In SQL Alchemy you are deleting Objects that you get with a query from the database. This you can do in 2 Ways:...
Read more >
Basic Insert Update and Delete with Dapper | Dave Paquette
The SQL statement is a simple DELETE with a WHERE clause on the Id column. To execute the delete, call the ExecuteAsync method...
Read more >
Asynchronous Programming - EF Core - Microsoft Learn
Asynchronous operations avoid blocking a thread while the query is executed in ... done by using the await keyword on each async operation....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found