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

How do I bulk delete with conditions? #403

Closed
8 tasks done
nghianv19940 opened this issue Aug 18, 2022 · 4 comments
Closed
8 tasks done

How do I bulk delete with conditions? #403

nghianv19940 opened this issue Aug 18, 2022 · 4 comments
Labels
question Further information is requested

Comments

@nghianv19940
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

@dialplan.put("/dialplans")
async def put_dialplan(extension_in: ExtensionIn, offset: int = 0,
                       limit: int = Query(default=100, lte=100),
                       session: Session = Depends(get_session)):
    # delete multiple dialplan records with condtions
    session.delete()

Description

I can't find any instruction on https://sqlmodel.tiangolo.com/tutorial/ for this, please help!.

Operating System

Linux

Operating System Details

No response

SQLModel Version

0.0.6

Python Version

3.10

Additional Context

No response

@nghianv19940 nghianv19940 added the question Further information is requested label Aug 18, 2022
@meirdev
Copy link

meirdev commented Aug 18, 2022

You can use delete from sqlalchemy:

from sqlalchemy import delete

stmt = delete(Item).where(Item.id.in_([1, 2, 3]))
session.exec(stmt)
session.commit()

@nghianv19940
Copy link
Author

Thanks @meirdev,

I've managed to do delete this way:

My model:

from sqlmodel import SQLModel, Field as sqlmodel_field

class Extension(SQLModel, table=True):
    \_\_tablename\_\_ = 'extensions'
    id: Optional[int] = sqlmodel_field(default=None, primary_key=True)
    context: str
    exten: str
    priority: str
    app: str
    appdata: str = None

Delete code:

session.execute(
        delete(Extension).where(
            col(Extension.exten).like(
                f'{my_var}%')).execution_options(
            synchronize_session="fetch"))

@tiangolo
Copy link
Member

Thanks for the help here @meirdev ! 👏 🙇

Thanks for reporting back and closing the issue @nghianv19940 👍

@mfornet
Copy link

mfornet commented Aug 9, 2024

Deleting this way doesn't pass type check. I have the following code

from sqlmodel import delete
session.exec(delete(Tags).where(Tags.registry_id == entry.id))

And pypy will fail with:

error: No overload variant of "exec" of "Session" matches argument type "Delete"  [call-overload]
hub/api/v1/registry.py:211: note: Possible overload variants:
hub/api/v1/registry.py:211: note:     def [_TSelectParam] exec(self, statement: Select[_TSelectParam], *, params: Mapping[str, Any] | Sequence[Mapping[str, Any]] | None = ..., execution_options: Mapping[str, Any] = ..., bind_arguments: dict[str, Any] | None = ..., _parent_execute_state: Any | None = ..., _add_event: Any | None = ...) -> TupleResult[_TSelectParam]
hub/api/v1/registry.py:211: note:     def [_TSelectParam] exec(self, statement: SelectOfScalar[_TSelectParam], *, params: Mapping[str, Any] | Sequence[Mapping[str, Any]] | None = ..., execution_options: Mapping[str, Any] = ..., bind_arguments: dict[str, Any] | None = ..., _parent_execute_state: Any | None = ..., _add_event: Any | None = ...) -> ScalarResult[_TSelectParam]

I see this is being discussed in #909

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

No branches or pull requests

4 participants