Note that there are some explanatory texts on larger screens.

plurals
  1. PODeleting an Object from Collection in SQLAlchemy
    primarykey
    data
    text
    <p>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):</p> <pre><code>assignees = relationship('Company', secondary=patent_company_table, backref='patents') </code></pre> <p>I am processing some of the objects stored in the database and for a Patent object <code>p</code>, I want to delete some assignee <code>a</code> (a Company object) from <code>p</code>'s assignee list. Based off of <a href="http://docs.sqlalchemy.org/en/latest/orm/session.html#deleting-from-collections" rel="nofollow">http://docs.sqlalchemy.org/en/latest/orm/session.html#deleting-from-collections</a> , it seems that calling <code>s.delete(a)</code> will actually delete the Company object <code>a</code>. I simply want to remove assignee <code>a</code> from the list of assignees for <code>p</code> (i.e. remove a row in the patent_company_table), NOT actually delete the Company object, because <code>a</code> might be in another Patent object's list of assignees.</p> <p>I tried creating a new list <code>new_assignees</code> that only includes the assignees from <code>p</code> besides <code>a</code> and then called:</p> <pre><code>p.assignees = new_assignees s.add(p) </code></pre> <p>This unfortunately does not actually mark <code>p</code> as dirty, so I assume it would not affect the database.</p> <p>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?</p> <p>Thank you.</p> <h2>UPDATE</h2> <p>Here is a snippet of the code:</p> <pre><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) </code></pre> <p>After looping through all of the patents, <code>added_patent_count = 983672</code> but there are no objects in <code>session.dirty()</code>. Do I need to add manually to the session after modifying via <code>append</code> or <code>remove</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload