On 08/24/2016 03:09 PM, Gordan Todorovac wrote:
Mike,


python -i ~/python/sq3.py
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
e = create_engine("sqlite:///:memory:")
Base.metadata.drop_all(e)
Base.metadata.create_all(e)
session=Session(e)
rory=User("rory")
session.add(rory)
chicken=Keyword("chicken")
session.add(chicken)
rory.keywords.append(chicken)
rory.keywords
[Keyword('chicken')]
chicken.users
[<__main__.User object at 0x7f82dff87390>]


Here, nothing is flushed yet. UserKeyword is a pending object and has not been inserted.


rory.keywords.remove(chicken)

Here, you've removed from User.user_keywords, marked the UserKeyword as deleted, and since it was never inserted, it's bumped out of the session.

rory.keywords
[]

rory.user_keywords is empty.

chicken.users
[None]

chicken.user_keywords still has the UserKeyword object stuck in it, the UserKeyword has no User attached to it, so the user name is None. There is no relationship between UserKeyword.keyword and UserKeyword.user, so you'd need to wait for the session to be expired and have this totally unrelated relationship load again, or perhaps use attribute events of your own to synchronize these ahead of re-loading.



session.flush()
/hostname/multiacl2/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py:235:
SAWarning: Object of type <UserKeyword> not in session, add operation
along 'Keyword.user_keywords' will not proceed

this warning is because this is an odd situation where you've added a UserKeyword as pending, never flushed it, and have now removed it from the session, but it's still sitting inside of a collection.


chicken.user_keywords
[<__main__.UserKeyword object at 0x7f82dfa6ded0>]

expected, same reasons above

chicken.user_keywords[0].user
chicken.user_keywords[0].keyword
Keyword('chicken')

expected, same reason above.

type "session.commit()".  Now everything is what you'd expect.

This is also explained in my answer at http://stackoverflow.com/a/14471166/34549.

The bug explained in that answer is that prior to 0.8, the UserKeyword object in question *would still be in the Session*, and since it's "user" is None, you get a NULL error - the original poster reported: "Causes an integrity error as SA tries to set one of the foreign key columns to null.". That's the part that was fixed.







So it definitely seems that the association object is
half-disassociated. I have this currently worked around by setting a
validator on UserKeyword.user that removes self from
UserKeyword.keyword.user_keywords if self.user is None, but I am not
confident in this workaround.



On Wednesday, August 24, 2016 at 1:57:58 PM UTC-4, Mike Bayer wrote:



    The issue mentioned in 2655 is a major behavior of the ORM, in that an
    object is considered orphan if any of its relationships are non-present
    in all cases, including pending and persistent, rather than if all of
    them are un-present.     This is not at all subtle and is covered in a
    wide range of test cases.   The test case attached to the bug continues
    to pass, as does the demonstration attached in the migration notes:
    
http://docs.sqlalchemy.org/en/latest/changelog/migration_08.html#the-consideration-of-a-pending-object-as-an-orphan-has-been-made-more-aggressive
    
<http://docs.sqlalchemy.org/en/latest/changelog/migration_08.html#the-consideration-of-a-pending-object-as-an-orphan-has-been-made-more-aggressive>.



    >
    >
    > Is anyone aware whether this bug was reintroduced on purpose (i.e.
    > figured out the rationale for the original behavior) or by accident?

    reintroduction of a bug here would need to be demonstrated.

--
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 sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to