deepcopy on CollectionAdapter result in exception

Issue #3852 resolved
ilang created an issue

version

SQLAlchemy (1.1.3)

Description

while calling deepcopy(destinationModel)
i get the following exception

File "research/detectors/tests/test_cnc_detector.py", line 391
in test_extend
    res = an.get_extension(Tick())
File "research/detectors/cnc_detector.py", line 271
in get_extension
    updated_destinations = deepcopy(self.destinations)
File "/anaconda/lib/python2.7/copy.py", line 190
in deepcopy
    y = _reconstruct(x, rv, 1, memo)
File "/anaconda/lib/python2.7/copy.py", line 352
in _reconstruct
    y.append(item)
File "/anaconda/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 1043
in append
    item = __set(self, item, _sa_initiator)
File "/anaconda/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 1015
in __set
    item = executor.fire_append_event(item, _sa_initiator)
File "/anaconda/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 678
in fire_append_event
    if self.invalidated:
E   AttributeError: invalidated

the destinationModel is of type InstrumentedList

it seems that the deepcopy doesnt copy the invalidated attribute due to the CollectionAdapter._getstate__

the invalidated attribute is used by CollectionAdapter.fire_append_event during the deepcopy , but this fails because the invalidated wasn't created

Comments (7)

  1. Mike Bayer repo owner

    please provide a proper test case.

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    
    
    class A(Base):
        __tablename__ = 'a'
        id = Column(Integer, primary_key=True)
        bs = relationship("B")
    
    class B(Base):
        __tablename__ = 'b'
        id = Column(Integer, primary_key=True)
        a_id = Column(ForeignKey('a.id'))
    
    
    
    a1 = A()
    import copy
    
    a2 = copy.deepcopy(a1)
    
    a2.bs.append(B())
    

    no error

  2. Mike Bayer repo owner

    Add missing items to collection.getstate

    the refactor in b606e47ddc54 / ticket:3457 failed to adjust getstate / setstate. need to memoize a few more things including the class itself so that we can navigate back to "attr".

    Change-Id: I4ece2a616cb8b9dac7b50763ca59e47d0f26cfdf Fixes: #3852

    → <<cset 3e063525f372>>

  3. Log in to comment