FlaskSQLAlchemy未保存数据库更新

2024-09-29 17:15:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将数据导入现有数据库项。我想为每个数据库项更新两个字段:一个字符串类型字段caled imagefolder和一个名为images的picketype(list)字段。两个字段的输入值都是存储文件路径的字符串。在

我的步骤是查询数据库项,检查其存在性,然后更新其字段。在

blend = Blend.query.filter_by(old_ID=row[4]).first()
if blend:
    blend.imagefolder = "/".join((row[16].split("/")[4:])[:-1])
    blend.images.append(ntpath.basename(row[16]))
    db.session.commit()

当我运行脚本时,什么都没有发生(数据库没有更新),我注意到导入脚本运行得非常快,太快了。在


Tags: 文件数据字符串路径脚本数据库类型步骤
1条回答
网友
1楼 · 发布于 2024-09-29 17:15:27

未能将更改的对象添加到当前会话。在

试试下面的片段。在

blend = db.session.query(Blend).filter_by(old_ID=row[4]).first()
if blend:
    blend.imagefolder = "/".join((row[16].split("/")[4:])[:-1])
    blend.images.append(ntpath.basename(row[16]))
    db.session.add(blend)
    db.session.commit()

相关问题 更多 >

    热门问题