Is multi-level polymorphism possible in SQLAlchemy?

Posted by Jace on Stack Overflow See other posts from Stack Overflow or by Jace
Published on 2010-05-18T16:53:56Z Indexed on 2010/05/18 18:10 UTC
Read the original article Hit count: 264

Filed under:
|

Is it possible to have multi-level polymorphism in SQLAlchemy? Here's an example:

class Entity(Base):
    __tablename__ = 'entities'
    id = Column(Integer, primary_key=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    entity_type = Column(Unicode(20), nullable=False)
    __mapper_args__ = {'polymorphic_on': entity_type}

class File(Entity):
    __tablename__ = 'files'
    id = Column(None, ForeignKey('entities.id'), primary_key=True)
    filepath = Column(Unicode(255), nullable=False)
    file_type = Column(Unicode(20), nullable=False)
    __mapper_args__ = {'polymorphic_identity': u'file', 'polymorphic_on': file_type)

class Image(File):
    __mapper_args__ = {'polymorphic_identity': u'image'}
    __tablename__ = 'images'
    id = Column(None, ForeignKey('files.id'), primary_key=True)
    width = Column(Integer)
    height = Column(Integer)

When I call Base.metadata.create_all(), SQLAlchemy raises the following error: NotImplementedError: Can't generate DDL for the null type IntegrityError: (IntegrityError) entities.entity_type may not be NULL. This error goes away if I remove the Image model and the polymorphic_on key in File.

What gives?

(Edited: the exception raised was wrong.)

© Stack Overflow or respective owner

Related posts about python

Related posts about sqlalchemy