Hello,

I am having a problem here - at one point a one-to-many relation
attribute is not in sync with a backref one (http://dpaste.com/104225/):

# Fails with Python-2.5.4 and SQLAlchemy-0.5.5 or SQLAlchemy-rel_0_5 rev
6312

import sqlalchemy
import sqlalchemy.ext.declarative

class TefDeclarativeMeta(sqlalchemy.ext.declarative.DeclarativeMeta):
    def __init__(cls, classname, bases, dict_):
        if '_decl_class_registry' in cls.__dict__:
            return type.__init__(cls, classname, bases, dict_)
        
        # added here because we need the first argument to relation()
dict_['manager'] = sqlalchemy.orm.relation(
    cls,
    backref = 'subordinates', 
    primaryjoin = cls.managerId == cls.Id,
    remote_side = cls.Id
)
        
        sqlalchemy.ext.declarative._as_declarative(cls, classname,
dict_)
        
        return type.__init__(cls, classname, bases, dict_)

Base =
sqlalchemy.ext.declarative.declarative_base(metaclass=TefDeclarativeMeta, 
mapper=sqlalchemy.orm.mapper)

class Employee(Base):
    __tablename__ = "employees"
    Id = sqlalchemy.Column( sqlalchemy.types.Integer, primary_key=True,
autoincrement=True)
    name = sqlalchemy.Column(sqlalchemy.types.String(128))
    managerId = sqlalchemy.Column(sqlalchemy.types.Integer,
sqlalchemy.ForeignKey(Id))

    def __repr__(self):
    return self.name

SteveBallmer = Employee(name = 'Steve Ballmer')
CraigMundie = Employee(name = 'Craig Mundie')
BillGates = Employee(name = 'Bill Gates')

CraigMundie.manager = BillGates
SteveBallmer.subordinates = [CraigMundie]

print "CraigMundie.manager: %s" % (CraigMundie.manager)

# Why does CraigMundie appear here?
# after SteveBallmer.subordinates = [CraigMundie] 
# CraigMundie.manager should be set to SteveBallmer so
# CraigMundie should not be present in BillGates.subordinates
print "BillGates.subordinates: %s" % (BillGates.subordinates)
print "SteveBallmer.subordinates: %s" % (SteveBallmer.subordinates)


Is this a SQLAlchemy bug or my mistake?

Regards,
Filip Zyzniewski


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to