Hi,

I have been reading the posts on implementing multiple inheritance in
SA. I have a question, for a slightly simpler method of MI, where
there is no overriding of attributes (and e.g. therefore no diamond
problem). This would mean that all underlying tables for classes could
contain columns for all attributes (including the inherited ones,
although these would also be attributes in the subclass). Hmm, ok,
example in code:

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relation, sessionmaker

Base = declarative_base()

class report(Base):
    __tablename__ = "reports"
    name = Column(String(50), primary_key=True)
    text = Column(String(1000), nullable = False)
    def __init__(self, name, text):
        self.name = name
        self.text = text
    def __repr__(self):
        return "report: " + self.name + ": " + self.text

class approvable(Base):
    __tablename__ = "approvals"
    id = Column(Integer, primary_key=True)
    approval = Column(Boolean)
    def __repr__(self):
        return "approved: " + self.approval

class approvable_report(approvable, report):
    __tablename__ = "approvable_reports"
    def __init__(self, name, text, appr):
        self.name = name
        self.text = text
        self.approval = appr
    def __repr__(self):
        return report.__repr__(self) + approvable.__repr__(self)


engine = create_engine('sqlite:///:memory:', echo=False)
Base.metadata.create_all(engine)

if __name__ == "__main__":
    Session = sessionmaker(bind=engine)
    session = Session()

    ar = approvable_report("repje", "this is the text of the report",
True)
    session.add(ar)
    session.commit()
    ar2 = session.query(approvable_report).first()
    print ar2

It would be fine if the approvable_reports table had all columns of
the superclasses (no overriding == no diamond problem), needing no
foreign keys (or am i missing something), as long as the multiple
inheritance relation in the python classes was maintained.  I get the
error:

Traceback (most recent call last):
  File "D:\Documents\Code\NetBeans\test\alchemy_test\src\alchemy.py",
line 31, in <module>
    class approvable_report(approvable, report):
  File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative.py",
line 1167, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative.py",
line 1099, in _as_declarative
    ignore_nonexistent_tables=True)
  File "C:\Python27\lib\site-packages\sqlalchemy\sql\util.py", line
260, in join_condition
    "between '%s' and '%s'.%s" % (a.description, b.description, hint))
sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships
between 'approvals' and 'approvable_reports'.

Is there any way to get this to work? I don't see the need for a
foreign key relationship between sub and superclasses in this case.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to