Add child classes to SQLAlchemy session using parent constructor
I have a class inheritance schema laid out at 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 would like to be able to create an instance Child
using a constructor Parent
(for example Parent(type='child')
), but it doesn't work. When I start 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 it possible? Is this a good idea?
source to share
Definitely not a good idea. Instead of using the constructor to do some hack, you can simply have a separate helper function (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 Parent
i will define the value for {'polymorphic_identity': 'parent'}
. This makes it much cleaner than None
.
EDIT-1: Using the constructor
Not what I recommend, or what I really know what I am doing here, but if you add __new__
as defined below to the class Parent
:
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 can either use it like the old way (when no type=xxx
is passed to __init__
), or do what you ask for by specifying a parameter:
old_parent = Parent(field1=xxx, ...)
old_child = Child(field1=xxx, ...)
new_child = Parent(type='child', field1=xxx, ...)
Again, I'm not sure of any implications, especially since sqlalchemy also overrides creation routines and uses its own meta classes.
source to share
The point is that when using declarative sqlalchemy mappings, a mapper is created for each class.
What you are trying to do is create a parent instance that will behave like a Child instance, which you cannot do, at least without resorting to hacks.
So (that you have to go through the hoops) is not a good idea. Maybe you don't need inheritance at all?
EDIT
If you don't want to have conditional logic or search queries and need to select a class based on user input, you can do something like this
cls = getattr(module_containing_the_classes, "<user_input>")
cls(**kw)
source to share