I am trying to my my joined inheritance code clearer, for the dynamic
generation of sa classes and tried to do something like this:


class InheritMixin(object):

    @declared_attr
    def __tablename__(cls):
        return cls.__name__
    @declared_attr
    def id(cls):
        if cls.__name__ == 'Object':
            return Column(Integer, primary_key = True)
        else:
            print 'in id: ', cls.__name__, cls.__bases__[0].__name__
            return Column(Integer,
ForeignKey(cls.__bases__[0].__name__ + '.id'), primary_key = True)
    @declared_attr
    def __mapper_args__(cls):
        if cls.__name__ == 'Object':
            return {'polymorphic_on': 'discriminator'}
        else:
            print 'in mapper_args: ', cls.__name__,
cls.__bases__[0].__name__
            return {'polymorphic_identity': cls.__name__,
                    'inherit_condition': (cls.id ==
cls.__bases__[0].id)}

Object = type('Object', (Base, InheritMixin), clsdict)

Where Object should be the (not necessarily direct) baseclass of all
inheriting classes. However I get errors: "Mapper Mapper|person|person
could not assemble any primary key columns for mapped table 'Join
object on Object(65389120) and person(65428224)' " etc ..

I noticed that the method __mapper_args__(cls) is always called before
id(cls) (which is never called, probably due to the error.

Is there some way to fix this, while keeping the inheritance code in a
mixin?

Also, is there a way to add the discriminator column to the mixin (if
i just directly add it to the declaration, this gave another maybe
related error)?

Cheers, Lars

-- 
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.

Reply via email to