Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is multi-level polymorphism possible in SQLAlchemy?

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:

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?

like image 503
Kiran Jonnalagadda Avatar asked May 18 '10 16:05

Kiran Jonnalagadda


2 Answers

Yes. The problem with your code is that you're making Image a type of file, when you must aim for the head of the tree, making Image a type of Entity.

Example:

from sqlalchemy import (Table, Column, Integer, String, create_engine,
    MetaData, ForeignKey)
from sqlalchemy.orm import mapper, create_session
from sqlalchemy.ext.declarative import declarative_base

e = create_engine('sqlite:////tmp/foo.db', echo=True)
Base = declarative_base(bind=e)

class Employee(Base):
    __tablename__ = 'employees'

    employee_id = Column(Integer, primary_key=True)
    name = Column(String(50))
    type = Column(String(30), nullable=False)

    __mapper_args__ = {'polymorphic_on': type}

    def __init__(self, name):
        self.name = name

class Manager(Employee):
    __tablename__ = 'managers'
    __mapper_args__ = {'polymorphic_identity': 'manager'}

    employee_id = Column(Integer, ForeignKey('employees.employee_id'),
        primary_key=True)
    manager_data = Column(String(50))

    def __init__(self, name, manager_data):
        super(Manager, self).__init__(name)
        self.manager_data = manager_data

class Owner(Manager):
    __tablename__ = 'owners'
    __mapper_args__ = {'polymorphic_identity': 'owner'}

    employee_id = Column(Integer, ForeignKey('managers.employee_id'),
        primary_key=True)
    owner_secret = Column(String(50))

    def __init__(self, name, manager_data, owner_secret):
        super(Owner, self).__init__(name, manager_data)
        self.owner_secret = owner_secret

Base.metadata.drop_all()
Base.metadata.create_all()

s = create_session(bind=e, autoflush=True, autocommit=False)    
o = Owner('nosklo', 'mgr001', 'ownerpwd')
s.add(o)
s.commit()
like image 105
nosklo Avatar answered Oct 19 '22 22:10

nosklo


Not possible (see SQL ALchemy doc):

Currently, only one discriminator column may be set, typically on the base-most class in the hierarchy. “Cascading” polymorphic columns are not yet supported.

So you should follow @nosklo proposal to change your heritage pattern.

like image 25
Matthieu P. Avatar answered Oct 19 '22 22:10

Matthieu P.