there is a lot lot lot going on here. The example isn’t working in 1.0 for
different reasons, for example.
However lets start with just the error you have, and to do that, lets please
just show the minimal amount of code to reproduce:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative import declared_attr, AbstractConcreteBase
Base = declarative_base()
class Mammut(Base):
__tablename__ = "mammut"
id = Column(Integer, primary_key=True)
nodes = relationship(
'TreeNode',
backref='mammut',
)
class TreeNode(AbstractConcreteBase, Base):
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
depth = Column(Integer, default=0)
data_type = Column(String(50))
@declared_attr
def mammut_id(cls):
return Column(Integer, ForeignKey('mammut.id'))
def __init__(self, name, value=None, parent=None):
self.name = name
self.parent = parent
self.depth = 0
self.value = value
if self.parent:
self.depth = self.parent.depth + 1
class IntTreeNode(TreeNode):
value = Column(Integer)
__tablename__ = 'int'
__mapper_args__ = {"concrete": True, "polymorphic_identity": 'int'}
class FloatTreeNode(TreeNode):
value = Column(Float)
miau = Column(String(50), default='zuff')
__tablename__ = 'float'
__mapper_args__ = {"concrete": True, "polymorphic_identity": 'float'}
node = IntTreeNode('rootnode', value=2)
mut = Mammut()
mut.nodes.append(node)
The issue is that you can’t just do a single “backref” to concrete classes.
You have to use the instructions at
http://docs.sqlalchemy.org/en/rel_0_9/orm/inheritance.html#relationships-with-concrete-inheritance
<http://docs.sqlalchemy.org/en/rel_0_9/orm/inheritance.html#relationships-with-concrete-inheritance>
to create each reference from IntTreeNode / FloatTreeNode etc. back.
This has been improved in 1.0. We can say:
class Mammut(Base):
__tablename__ = "mammut"
id = Column(Integer, primary_key=True)
nodes = relationship(
'TreeNode',
back_populates='mammut',
)
class TreeNode(AbstractConcreteBase, Base):
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
depth = Column(Integer, default=0)
data_type = Column(String(50))
@declared_attr
def mammut_id(cls):
return Column(Integer, ForeignKey('mammut.id'))
@declared_attr
def mammut(cls):
return relationship("Mammut", back_populates='nodes')
def __init__(self, name, value=None, parent=None):
self.name = name
self.parent = parent
self.depth = 0
self.value = value
if self.parent:
self.depth = self.parent.depth + 1
however in 0.9, this won’t work, and after some experimentation I don’t think
it’s possible in 0.9 to have a backref pointing to an AbstractConcreteBase,
sorry.
Also, the attempt to make a self-referential relationship from TreeNode to
itself is also not possible in the way you are attempting. There is no
TreeNode table, so this would require distinct relationships and foreign keys
on each concrete table, however I’m not getting that to work either.
I think you might not intend to use AbstractConcreteBase here in any case as it
seems like you want there to be a base table (this creates one?).
I’ll try to look more later but overall there’s kind of too much going on here
and concrete inheritance is not very easy to use, sorry.
> On Nov 4, 2014, at 7:28 AM, delijati <[email protected]> wrote:
>
> Hello,
>
> i posted my question on stakoverflow. So to not repeat myself:
>
> https://stackoverflow.com/questions/26724897/adjacency-list-abstract-base-class-inheritance-used-in-relationship
>
> Josip
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/sqlalchemy
> <http://groups.google.com/group/sqlalchemy>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.