Note that there are some explanatory texts on larger screens.

plurals
  1. POAssociation Proxy SQLAlchemy
    primarykey
    data
    text
    <p><a href="http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#creation-of-new-values" rel="noreferrer">This source</a> details how to use association proxies to create views and objects with values of an ORM object.</p> <p>However, when I append an value that matches an existing object in the database (and said value is either unique or a primary key), it creates a conflicting object so I cannot commit.</p> <p>So in my case is this only useful as a view, and I'll need to use ORM queries to retrieve the object to be appended.</p> <p>Is this my only option or can I use merge (I may only be able to do this if it's a primary key and not a unique constraint), OR set up the constructor such that it will use an existing object in the database if it exists instead of creating a new object?</p> <p>For example from the docs:</p> <pre><code>user.keywords.append('cheese inspector') # Is translated by the association proxy into the operation: user.kw.append(Keyword('cheese inspector')) </code></pre> <p>But I'd like to to be translated to something more like: (of course the query could fail).</p> <pre><code>keyword = session.query(Keyword).filter(Keyword.keyword == 'cheese inspector').one() user.kw.append(keyword) </code></pre> <p>OR ideally</p> <pre><code>user.kw.append(Keyword('cheese inspector')) session.merge() # retrieves identical object from the database, or keeps new one session.commit() # success! </code></pre> <p>I suppose this may not even be a good idea, but it could be in certain use cases :)</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.
    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