使用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.
更多推荐
发布评论