On 5/23/15 12:30 PM, [email protected] wrote:
It cost me some time to analyse this problem. ;) The problem in the pseudo-code below is that 'obj' lose the reference to its foreign-key object (set with relationship()). The foreign key member itself is still correct set with a numeric value. I understand that this can happen because of the reference cycle counting stuff in the background of SQLA. But how can I prevent this? obj = GetObjFromDb() make_transient(obj) obj._pk = None # This can return 'None' in some cases obj.GetForeignKeyObject()
don't do that. Put the object back into a session and get it to be associated with a real row in the DB again.
the "load_on_pending" flag was added for those users who really want "obj.some_relationship" to actually load something when the object isn't INSERTed yet, but you still need to be associated with a Session for it to load something, and it also needs to have the relevant column-holding attributes populated for that to work.
Yet another option for people who really wanted to do things in not the way I recommend is the enable_relationship_loading() method: http://docs.sqlalchemy.org/en/rel_1_0/orm/session_api.html#sqlalchemy.orm.session.Session.enable_relationship_loading. This method associates a transient object with the session in such a way that it is not even pending; it's still technically transient. The session is just there for the purposes of lazy loading on relationships that happen to match up with values you've placed in the column holding attributes.
Both load_on_pending and enable_relationship_loading were added mostly at the request of one or two users with some existing crazy architecture I want nothing to do with. I've never needed either of these methods :).
-- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
