简体   繁体   English

fastAPI SQLmodel MultipleResultsFound:当恰好需要一行时找到多行

[英]fastAPI SQLmodel MultipleResultsFound: Multiple rows were found when exactly one was required

This is my delete function.这是我删除的 function。

def delete_session(self,session_id: int, db):
        with Session(engine) as session:
            statement = select(db).where(db.session == session_id)
            results = session.exec(statement)
            sess = results.one()
            print("sess: ", sess)
        if not sess:
            raise HTTPException(status_code=404, detail="Session not found")
        session.delete(sess)
        session.commit()
        return {"Session Deleted": True}

I want to delete all records where session_id matches.我想删除 session_id 匹配的所有记录。 But its throwing following error MultipleResultsFound: Multiple rows were found when exactly one was required但它抛出以下错误MultipleResultsFound: Multiple rows were found when exactly one was required

How can i delete multiple rows at once.我怎样才能一次删除多行。

I tried using我尝试使用

sess = results.all()

but it say但它说

sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.list' is not mapped

Thanks谢谢

Currently, you are trying to delete several data items, except that session.delete() only takes a single value, not a list of values.当前,您正在尝试删除多个数据项,但session.delete()仅采用单个值,而不是值列表。

You are using results.one() probably thinking that you can isolate your answers and return only one.您正在使用results.one()可能认为您可以隔离您的答案并只返回一个。 However, it is explained in the documentation that if multiple entries are found in the parameter passed to one() then it will throw a MultipleResultsFound exception, hence your error.但是,文档中解释说,如果在传递给one()的参数中找到多个条目,那么它将抛出MultipleResultsFound异常,因此会出现错误。

Indeed, your statement returns a list, so multiple values.实际上,您的语句返回一个列表,因此有多个值。

In order to delete all your elements, you should not use one() but simply iterate with a for loop on your results and delete one by one, your data, as follows:为了删除所有元素,您不应该使用one()而是简单地用for循环迭代您的results并一个一个地删除您的数据,如下所示:


def delete_session(self, session_id: int, db):
    with Session(engine) as session:
        statement = select(db).where(db.session == session_id)
        results = session.exec(statement).all()
        for sess in results:
            session.delete(sess)
    session.commit()
    return {"Session Deleted": True}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 fastapi 和 sqlmodel 的 BigInteger - BigInteger with fastapi and sqlmodel 如何在fastapi python框架中使查询参数列表(多个值)至少需要一个值 - How to make query parameter list (multiple values) required at least one value in fastapi python framework 如何使用 FastAPI、SQLalchemy 和 SQLModel 生成 UUID 字段 - How to generate a UUID field with FastAPI, SQLalchemy, and SQLModel FastAPI + SQLModel 开发中的简洁架构原则 - Clean architecture principles in FastAPI + SQLModel development 未创建具有 SQLModel 用户表的数据库适配器的 fastapi fastapi-users - fastapi fastapi-users with Database adapter for SQLModel users table is not created 一个端点fastapi中的多个功能 - multiple functions in one endpoint fastapi SqlModel:Fastapi AttributeError:类型 object 'AddressBaseCore' 没有属性 '__config__' - SqlModel : Fastapi AttributeError: type object 'AddressBaseCore' has no attribute '__config__' 从 sqlmodel 获取连接表作为 fastapi 中的嵌套响应 model - getting joined tables from sqlmodel as a nested responde model in fastapi 无法通过 SQLModel 将 map FastAPI List[]、Set() 转入 Postgres - Unable tu map FastAPI List[], Set() through SQLModel into Postgres 在 FastAPI 和 SQLModel 提供的 OpenAPI 接口中获取嵌套(连接)表 - Getting nested (joined) tables to display in the OpenAPI interface provided by FastAPI and SQLModel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM