Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instance.delete() not work as expected #898

Closed
JoshYuJump opened this issue Nov 3, 2023 · 2 comments
Closed

instance.delete() not work as expected #898

JoshYuJump opened this issue Nov 3, 2023 · 2 comments

Comments

@JoshYuJump
Copy link

I am working on a API of deleting a single resource named product.

DELETE /products/1

Part of the code is

@router.delete('/product/{row_id}')
async def delete_product(row_id: str):
    product = await Product.objects().where(Product.id == row_id)
    await product.delete()

It don't work and raise a exception remind me added where clause

raise DeletionError(
                "Do you really want to delete all the data from "
                f"{classname}? If so, use {classname}.delete(force=True). "
                "Otherwise, add a where clause."
            )
@sinisaos
Copy link
Member

sinisaos commented Nov 3, 2023

@JoshYuJump You have 2 ways to delete a record.

  1. As a query builder with delete method
@router.delete('/product/{row_id}')
async def delete_product(row_id: str):
    await Product.delete().where(Product.id == row_id)
  1. As an ORM with objects
@router.delete('/product/{row_id}')
async def delete_product(row_id: str):
    product = await Product.objects().where(Product.id == row_id).first()
    # you can also use get() method like this to get single object
    # product = await Product.objects().get(Product.id == row_id)
    await product.remove() # a proxy to a delete query

Hope that helps.

@JoshYuJump
Copy link
Author

@sinisaos Thanks a lot, It was so helpful to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants