[Sqlalchemy-tickets] Issue #3636: abstract class inheritors not behaving properly (zzzeek/sqlalchem
Brought to you by:
zzzeek
|
From: Daniel K. <iss...@bi...> - 2016-01-22 01:31:03
|
New issue 3636: abstract class inheritors not behaving properly https://bitbucket.org/zzzeek/sqlalchemy/issues/3636/abstract-class-inheritors-not-behaving Daniel Kassen: Below is a snippet of code that defines a Parent class and a Child class which is abstract and used for inheritance. Classes that inherit from Child do not automatically populate if the entry exists in the database. By that, I mean the proper query is not performed to automatically pull that object out from the database even though it exists, causing duplicate entry errors when commit()'ing the session. ``` #!python class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=true) name = Column(String(100, collation='utf8_bin'), nullable=False, unique=True) class Child(Base): __abstract__ = True @classmethod def _classname(cls): return cls.__name__ @delcared_attr def __tablename__(self): return self._classname().lower() @delcared_attr def id(self): return Column(Integer, ForeignKey(Parent.id), primary_key=True) @declared_attr def parent(self): return relationship(Parent) ``` Creating a factory that declares a class with the same attributes as are declared above in the Child class actually works, which is why this isn't very high priority, but it does make my code much less pythonic. Classes created from the factory below auto-populate as they should. ``` #!python def create_child(name): class Child(Base): __tablename__ = name.lower() id = Column(Serial, ForeignKey(Parent.id), primary_key=True) parent = relationship(Parent) Child.__name__ = name return Child ``` Responsible: zzzeek |