sqlalchemy.exc.InvalidRequestError: Instance is not persisted

本文介绍如何使用Flask框架及SQLAlchemy ORM正确地从数据库中删除记录,包括常见错误及解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

flask_restful 想完成从数据库中删除记录操作

文档中描述如下:

Deleting Records

Deleting records is very similar, instead of add() use delete():

>>> db.session.delete(me)
>>> db.session.commit()

实现代码如下

    def delete(self, id):
        fid = request.form['data']
        friend = Friends(userid=id, friendid=fid)
        db.session.delete(friend)
        db.session.commit()

报错:sqlalchemy.exc.InvalidRequestError: Instance '<Friends at 0x35348d0>' is not persisted


在StackOverflow中看到:

You are trying to delete a new instance, rather than an instance you got from the database. Either you meant to use db.session.add(), or you meant to use user = User.query.filter_by(email='john@john.com').first() (or something similar) and delete that.

You might also have trouble accessing attributes of the deleted instance (assuming you delete it correctly as above). When you commit a session, all instances in that session are expired. Trying to access an expired attribute triggers a database lookup, but there can be no lookup for this object because it was deleted.

You can turn expire_on_commit off, but that is not the normal behavior and will probably cause you other problems.

You could also try calling make_transient on it.

Ultimately, you should really just abandon instances that have been deleted. There is probably a better way to do whatever you are trying to accomplish.

链接:https://stackoverflow.com/questions/26597755/invalidrequesterror-instance-user-at-0x7f65938a7510-is-not-persisted

原因是创建的friend对象在数据库中并不存在,改为:

    def delete(self, id):
        fid = request.form['data']
        # friend = Friends(userid=id, friendid=fid)
        friend = Friends.query.filter_by(userid=id, friendid=fid).first()
        db.session.delete(friend)
        db.session.commit()
问题解决

本文档的主要内容详细介绍的是MPU9250芯片的相关资料合集,主要内容包括了:使用MPU9250芯片进行的运动驱动器程序,英文森运动传感器通用评估委员会(UEVB)用户指南,MPU-9250产品中文说明书,MPU-9250产品英文数据手册,MPU-9250寄存器和说明,叉积法融合陀螺和加速度核心程序详解,姿态解算说明   MPU9250是一-个QFN封装的复合芯片(MCM), 它由2部分组成。一组是3轴加速度还有3轴陀螺仪,另一组则是AKM公司的AK8963 3轴磁力计。所以,MPU9250是一款9轴运动跟踪装置,他在小小的3X3X1mm的封装中融合了3轴加速度,3 轴陀螺仪以及数字运动处理器(DMP) 并且兼容MPU6515。其美的I2C方案,可直接输出9轴的部数据。一体化的设计,运动性的融合,时钟校准功能,让开发者避开了繁琐复杂的芯片选择和外设成本,保证最佳的性能。本芯片也为兼容其它传感器开放了辅助12C接口,比如连接压力传感器。   MPU9250的具有三个16位加速度AD输出,三个16位陀螺仪AD输出,三个6位磁力计AD输出。精密的慢速和快速运动跟踪,提供给客户量程的可编程陀螺仪参数选择(土250,土500,士1000, and土2000 。/秒(dps)),可编程的加速度参数选择土2g,土4g,土8g, 土16g, 以及最大磁力计可达到土4800uT。   其他业界领先的功能还有可编程的数字滤波器, 40-85 C时带高精度的1%的时钟漂移,嵌入了温度传感器,并且带有可编程中断。该装置提供12C和SPI的接口,2. 4-3. 6V的供电电压,还有单独的数字10口,支持1.71V到VDD。   通信采用400KHz的12C和1MHz的SPI,若需要更快的速度,可以用SPI在20MHz的RDDLER RIDLER模式下直接读取传感器和中断寄存器。   采用CMOS-MEMS的制作平台,让传感器以低成本的高性能集成在- -个3x3x1mm的芯片内,并且能承受住10, 000g 的震动冲击。 应用领域 无需触碰操作的技术 手势控制 体感游戏控制器 位置查找服务 手机等便携式游戏设备 PS4或XBOX等游戏手柄控制器 3D电视遥控器或机顶盒 3D鼠标 可穿戴的健康智能设备
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值