使用db.commit()sqlalchemy PYTHON在循环中添加数据(add data in loop with db.commit() sqlalchemy PYTHON)

编程入门 行业动态 更新时间:2024-10-26 10:31:14
使用db.commit()sqlalchemy PYTHON在循环中添加数据(add data in loop with db.commit() sqlalchemy PYTHON)

我需要Python代码的帮助

copy_groups = Group() groups = Group.query.filter(Group.project_id == curent_project_id) for group in groups: copy_groups.project_id = curent_project.id copy_groups.name = group.name db.session.add(copy_groups) db.session.commit()

当我使用这个代码时,它只添加了最后一个group ,但我需要将所有group添加到group中。

当我使用

copy_groups = Group() groups = Group.query.filter(Group.project_id == curent_project_id) for group in groups: copy_groups.project_id = curent_project.id copy_groups.name = group.name db.session.add(copy_groups) db.session.commit()

我有错误。

但我需要将所有group groups

请帮我解决这个问题。 谢谢。

I need help with Python code

copy_groups = Group() groups = Group.query.filter(Group.project_id == curent_project_id) for group in groups: copy_groups.project_id = curent_project.id copy_groups.name = group.name db.session.add(copy_groups) db.session.commit()

When I used this code it was added only last group, but I need add all group in groups.

When I used

copy_groups = Group() groups = Group.query.filter(Group.project_id == curent_project_id) for group in groups: copy_groups.project_id = curent_project.id copy_groups.name = group.name db.session.add(copy_groups) db.session.commit()

I had error.

But I need add all group in groups

Please help me with this problem. Thank you.

最满意答案

我的代码是为了复制项目,而我从未发布的代码是一行代码

copy_project.create_default_groups (current_user, tenant = tenant)

由于其中的复制被重复进行,这就是为什么所有Feil。 删除这条线,一切工作正常。

My code is meant to copy the project, and the code that I never published, was a line

copy_project.create_default_groups (current_user, tenant = tenant)

due to which the copying was carried out duplication, which is why all Feil. removing this line, everything works correctly.

更多推荐

本文发布于:2023-08-05 22:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   sqlalchemy   db   commit   PYTHON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!