Hi all,
i have a problem with attribute inheritance as seen here:
========================================================
# Fails with Python-2.5.4 and SQLAlchemy-0.5.5
# There is happening something very strange, which causes
DeliveryTask.result not to cover Task.result even though DeliveryTask is
a child of Task.
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_)
base = bases[0]
cls.__tablename__ = cls.__name__
if 'Id' not in dict_.keys():
cls.Id = sqlalchemy.Column(sqlalchemy.types.Integer,
sqlalchemy.ForeignKey(base.Id), primary_key=True)
if hasattr(base, 'Id'):
cls.__mapper_args__ = {'inherit_condition': cls.Id ==
base.Id, 'polymorphic_identity': cls.__name__}
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 TefEx(object):
def __init__(self, enumDict):
self.enumDict = enumDict
def set(self, state, value, oldvalue, initiator):
import sys; sys.stderr.write("SET %s on %s\n" % (value,
self.enumDict))
if value not in self.enumDict.keys():
raise ValueError("value %s not in %s" % (value,
self.enumDict))
return value
class Task(Base):
Id = sqlalchemy.Column( sqlalchemy.types.Integer, primary_key=True,
autoincrement=True)
objectType = sqlalchemy.Column( sqlalchemy.types.String(128),
nullable=False)
__mapper_args__ = {'polymorphic_on': objectType}
result =
sqlalchemy.orm.column_property(sqlalchemy.Column(sqlalchemy.types.Integer),
extension = TefEx({0: 'Success', 1: 'Failure'}))
class DeliveryTask(Task):
result =
sqlalchemy.orm.column_property(sqlalchemy.Column(sqlalchemy.types.Integer),
extension = TefEx({0: 'Delivered', 1: 'Rejected', 2: 'Redirected', 3:
'Recipient dead'}))
task = DeliveryTask()
task.result = 3
========================================================
An extension doesn't get inherited properly - a child class instance
still uses parent class extension...
Am I doing something wrong here? Is there a proper way to redefine
extensions?
thanks for reading this,
Filip Zyzniewski
Tefnet
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---