3
votes

I have a class inheritance scheme as layed out in http://docs.sqlalchemy.org/en/latest/orm/inheritance.html#joined-table-inheritance

from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()


class Parent(Base):
    __tablename__ = 'parent'

    id = Column(Integer, primary_key=True)
    type = Column(String)

    __mapper_args__ = {'polymorphic_on': type}


class Child(Parent):
    __tablename__ = 'child'

    id = Column(Integer, ForeignKey('parent.id'), primary_key=True)

    __mapper_args__ = {'polymorphic_identity': 'child'}

I'd like to be able to create an instance of Child using the constructor of Parent (like Parent(type='child')) but it doesn't work. When I fire up IPython...

In [1]: from stackoverflow.question import Parent, Child

In [2]: from sqlalchemy import create_engine

In [3]: from sqlalchemy.orm import sessionmaker

In [4]: session = sessionmaker(bind=create_engine(...), autocommit=True)()

In [5]: with session.begin():
    p = Parent(type='child')
    session.add(p)
   ...:     
/.../lib/python3.4/site-packages/sqlalchemy/orm/persistence.py:155: SAWarning: Flushing object <Parent at 0x7fe498378e10> with incompatible polymorphic identity 'child'; the object may not refresh and/or load correctly
  mapper._validate_polymorphic_identity(mapper, state, dict_)

In [6]: session.query(Parent).all()
Out[6]: [<stackoverflow.question.Parent at 0x7fe498378e10>]

In [7]: session.query(Child).all()
Out[7]: []

Is this possible? Is it a good idea?

2
It would be easier to tell you if it's a good idea if you were a little more specific about the end result you're after. Intuitively it seems like you only need a foreign key but you're using a parent-child relationship, and thus it could be handled more simply (probably with more intuitive results). - Two-Bit Alchemist
The end result I want is something like this: based on user input I create either ChildA or ChildB which both inherit from Parent but have different attributes. I'd prefer to have that all occur in one step rather than using conditional logic or a lookup table. - user1475412

2 Answers

2
votes

Definitely not a good idea. Instead of using a constructor to do some hack, you could just have a separate helper function (a factory):

# create this manually
OBJ_TYPE_MAP = {
    # @note: using both None and 'parent', but should settle on one
    None: Parent, 'parent': Parent,
    'child': Child,
}
# ... or even automatically from the mappings:
OBJ_TYPE_MAP = {
    x.polymorphic_identity: x.class_
    for x in Parent.__mapper__.self_and_descendants
}
print(OBJ_TYPE_MAP)


def createNewObject(type_name, **kwargs):
    typ = OBJ_TYPE_MAP.get(type_name)
    assert typ, "Unknown type: {}".format(type_name)
    return typ(**kwargs)


a_parent = createNewObject(None, p_field1='parent_name1')
a_child = createNewObject(
    'child', p_field1='child_name1', c_field2='child_desc')

session.add_all([a_child, a_parent])

Another note: for the Parent i would define a value for {'polymorphic_identity': 'parent'}. It makes it much cleaner than having None .

EDIT-1: using Constructor

Not that i recommend it, or that I really know what I am doing here, but if you add the __new__ as defined below to the Parent class:

def __new__(cls, *args, **kwargs):
    typ = kwargs.get('type')  # or .pop(...)

    if typ and not kwargs.pop('_my_hack', None):
        # print("Special handling for {}...".format(typ))

        if typ == 'parent':
            # here we can *properly* call the next in line
            return super(Parent, cls).__new__(cls, *args, **kwargs)
        elif typ == 'child':
            # @note: need this to avoid endless recursion
            kwargs["_my_hack"] = True
            # here we need to cheat somewhat
            return Child.__new__(Child, *args, **kwargs)
        else:
            raise Exception("nono")
    else:
        x = super(Parent, cls).__new__(cls, *args, **kwargs)

    return x

you will be able to use both the old way (when no type=xxx is passed to __init__), or do what you ask for by providing a parameter:

old_parent = Parent(field1=xxx, ...)
old_child = Child(field1=xxx, ...)
new_child = Parent(type='child', field1=xxx, ...)

Again, I am not sure of all implications, especially because sqlalchemy also overrides the creation routines and uses its own meta classes.

1
votes

The thing is that when using sqlalchemy declarative mappings a mapper is generated for each class.

What you are trying to do is to make an instance of Parent that will behave as an instance of Child, which is something you can't do, at least without resorting to hacks.

By that fact (that you have to go through hoops) it's not a good idea. Maybe you don't need inheritance at all ?

EDIT

If you don't want to have conditional logic or lookups and you have to select a class based on user input you could do something like this

cls = getattr(module_containing_the_classes, "<user_input>") 
cls(**kw)