On Mar 1, 2011, at 5:42 PM, farcat wrote:

> 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)
> 
> 
> 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.

While the error you're getting is a small user error in that you didn't add any 
columns to "approvable_reports" (and yes, joined inheritance needs to join the 
three tables together so foreign keys are needed), that's not ultimately what 
would prevent this from working.

SQLAlchemy mappers track the structure of an inheritance hierarchy strictly as 
a tree structure where each node has a single parent, and the assumption of 
this single parent at most is prevalent through every area of the ORM.  So 
there's no way to make the above pattern work without major reworkings of 
SQLAlchemy internals.   Such a pattern of tables would instead need to use 
relationship() or similar to map the underlying structures, then produce a 
facade on top.

Mapping multiple inheritance via separate tables in a relational database is 
not very practical in general - simple joined table inheritance already 
produces a large number of joins and subqueries very quickly and is already 
cumbersome from a SQL perspective.   




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

Reply via email to