add per-mapper dependencies even if no detected state change on relationship

Issue #2002 resolved
Mike Bayer repo owner created an issue
########
# randomize sets
from sqlalchemy.orm import unitofwork, session, mapper, dependency
from sqlalchemy import topological
from sqlalchemy.test.util import RandomSet
topological.set = unitofwork.set = session.set = mapper.set = dependency.set = RandomSet
##########

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, Session
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.types import Integer

engine = create_engine('postgresql://scott:tiger@localhost/test', echo=True)

Model = declarative_base()

class Parent(Model):
   __tablename__ = 'parents'

   id = Column(Integer, primary_key=True)


class Child(Model):
   __tablename__ = 'children'

   id = Column(Integer, primary_key=True)
   parent_id = Column(Integer, ForeignKey('parents.id'),
                      nullable=False)
   parent = relationship('Parent')


Model.metadata.create_all(engine)

parent = Parent()
children = [Child(parent=parent)](Child(parent=parent),)

session = Session(engine)

try:
   session.add_all(children)
   session.add(parent)
   session.commit()


   for child in children:
       session.delete(child)
   session.delete(parent)
   session.commit()
finally:
   Model.metadata.drop_all(engine)

Comments (3)

  1. Mike Bayer reporter

    potential patch. leave it up to "passive_deletes" as always:

    diff -r 0bceb861bffc40d22f1bb75517410be20446cd1d lib/sqlalchemy/orm/dependency.py
    --- a/lib/sqlalchemy/orm/dependency.py  Wed Dec 15 13:25:16 2010 -0500
    +++ b/lib/sqlalchemy/orm/dependency.py  Wed Dec 15 15:27:14 2010 -0500
    @@ -221,6 +221,8 @@
             pass
    
         def prop_has_changes(self, uowcommit, states, isdelete):
    +        passive = not isdelete or self.passive_deletes
    +        
             for s in states:
                 # TODO: add a high speed method 
                 # to InstanceState which returns:  attribute
    @@ -228,7 +230,7 @@
                 history = uowcommit.get_attribute_history(
                                                 s, 
                                                 self.key, 
    -                                            passive=True)
    +                                            passive=passive)
                 if history and not history.empty():
                     return True
             else:
    

    this approach maintains the unit tests including the ones that test the size of the uow. It also handles the self referential case.

  2. Log in to comment