setting child backref attribute failed when lazy=None

Issue #813 resolved
Former user created an issue

SA version 0.3.10.

In my case a mapper of a parent object specifies lazy=None, and a backref attribute for a child object. Then when I tries to set this attribute of a child object the exception occurs. I guess it is because parent didn't setup the list of child objects.

Also, in some previous version of SA this approach works fine.

Comments (4)

  1. Mike Bayer repo owner

    the posted test case is too long and involved...pls confirm this is the failing condition:

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy import types
    
    engine = create_engine('sqlite://', echo=True)
    
    metadata = MetaData(engine)
    
    users = Table('users', metadata,
        Column('id', types.Integer, primary_key=True),
        Column('name', types.String(30), nullable=False),
        Column('password', types.String(15))
    )
    
    addresses = Table('addresses', metadata,
        Column('id', types.Integer, primary_key=True),
        Column('user_id', types.Integer,
             ForeignKey('users.id'), nullable=False),
        Column('email_address', types.String(100),
             nullable=False)
    )
    
    metadata.create_all()
    
    class User(object):
        def __init__(self, name, password):
            self.name = name
            self.password = password
    
        def __repr__(self):
            return "User(%r, %r)" % (self.name, self.password)
    
    
    class Address(object):
        def __init__(self, email_address):
            self.email_address = email_address
    
        def __repr__(self):
            return "Address(%r)" % self.email_address
    
    mapper(User, users, properties={
        'addresses':relation(Address, backref='user', lazy=None)
    })
    mapper(Address, addresses)
    
    sess = create_session()
    
    # try it on unsaved objects
    u1 = User('u1', 'foo')
    a1 = Address('a1')
    a1.user = u1
    sess.save(u1)
    sess.flush()
    sess.clear()
    
    a1 = sess.query(Address).get(a1.id)
    
    a1.user = None
    
  2. Former user Account Deleted

    Replying to zzzeek:

    the posted test case is too long and involved...pls confirm this is the failing condition:

    Yes, this is the one. The stack trace is below:

    Traceback (most recent call last):
      File "./tmp.py", line 60, in ?
        a1.user = None
      File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/attributes.py", line 45, in __set__
        self.set(None, obj, value)
      File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/attributes.py", line 271, in set
        ext.set(event or self, obj, value, old)
      File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/attributes.py", line 646, in set
        getattr(oldchild.__class__, self.key).remove(event, oldchild, obj)
      File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/attributes.py", line 332, in remove
        self.get(obj).remove_with_event(value, event)
      File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/attributes.py", line 516, in remove_with_event
        self.data.remove(item)
    ValueError: list.remove(x): x not in list
    

    Thanks.

  3. Log in to comment