从SQLAlchemy中的集合中删除对象

编程入门 行业动态 更新时间:2024-10-23 04:45:06
本文介绍了从SQLAlchemy中的集合中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将一堆专利数据存储在MySQL数据库中,并通过SQLAlchemy与之交互.我在Patent类中有一个集合,该集合表示受让人(被授予专利的公司)的列表:

assignees = relationship('Company', secondary=patent_company_table, backref='patents')

我正在处理存储在数据库中的某些对象,对于专利对象p,我想从p的受让人列表中删除某些受让人a(公司对象).基于 docs.sqlalchemy/zh_CN/latest/orm/session.html#deleting-from-collections ,似乎调用s.delete(a)实际上会删除Company对象a.我只想从p的受让人列表中删除受让人a(即,删除patent_company_table中的一行),而不是实际上删除Company对象,因为a可能在另一个Patent对象的受让人列表中. /p>

我尝试创建一个新列表new_assignees,该列表仅包含a之外的p中的受让人,然后调用:

p.assignees = new_assignees s.add(p)

不幸的是,这实际上并未将p标记为脏,因此我认为它不会影响数据库.

对于从集合中删除对象,删除Patent_company_table中的行而不是从Company表中删除对象,您有什么建议吗?

谢谢.

更新

以下是代码段:

assignees = patent.assignees for assignee in assignees: if assignee in duplicate_company_to_default: patent.assignees.remove(assignee) default_company = duplicate_company_to_default[assignee] if default_company not in assignees: added_patent_count += 1 patent.assignees.append(default_company)

遍历所有专利后,added_patent_count = 983672,但session.dirty()中没有任何对象.通过append或remove修改后,我是否需要手动添加到会话中?

解决方案

SQLAlchemy集合支持类似列表的附加/删除操作.

p.assignees.remove(c)

这应该从数据库中删除c形式的p.assignees,而不从数据库中删除c.

I am storing a bunch of patent data in a MySQL database and interacting with it via SQLAlchemy. I have a collection inside the Patent class that represents the list of assignees (the companies that were assigned the patent):

assignees = relationship('Company', secondary=patent_company_table, backref='patents')

I am processing some of the objects stored in the database and for a Patent object p, I want to delete some assignee a (a Company object) from p's assignee list. Based off of docs.sqlalchemy/en/latest/orm/session.html#deleting-from-collections , it seems that calling s.delete(a) will actually delete the Company object a. I simply want to remove assignee a from the list of assignees for p (i.e. remove a row in the patent_company_table), NOT actually delete the Company object, because a might be in another Patent object's list of assignees.

I tried creating a new list new_assignees that only includes the assignees from p besides a and then called:

p.assignees = new_assignees s.add(p)

This unfortunately does not actually mark p as dirty, so I assume it would not affect the database.

Do you have any suggestions for how to remove an object from the collection, deleting the row in the patent_company_table as opposed to deleting the object from the Company table?

Thank you.

UPDATE

Here is a snippet of the code:

assignees = patent.assignees for assignee in assignees: if assignee in duplicate_company_to_default: patent.assignees.remove(assignee) default_company = duplicate_company_to_default[assignee] if default_company not in assignees: added_patent_count += 1 patent.assignees.append(default_company)

After looping through all of the patents, added_patent_count = 983672 but there are no objects in session.dirty(). Do I need to add manually to the session after modifying via append or remove?

解决方案

SQLAlchemy collections support list-like append/remove operations.

p.assignees.remove(c)

This should remove c form p.assignees without deleting c from database.

更多推荐

从SQLAlchemy中的集合中删除对象

本文发布于:2023-10-23 13:53:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521010.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   SQLAlchemy

发布评论

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

>www.elefans.com

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