On Jan 9, 2014, at 11:32 AM, Enrico Bottani <bei...@mac.com> wrote:

> Hello guys,
> 
> I'm building a web service to provide data to our SDK and I'm facing a wired 
> problem. I have created 4 different models for my needs, a base class that is 
> called Unit, two derivatives from the base class VideoUnit, QuestionUnit, and 
> the last class is a derivative of both VideoUnit and QuestionUnit, that is 
> called, with lot of fantasy, VideoQuestionUnit.
> Everything was working fine till the point I was trying to serialize this 
> data to be transmitted with JSON.
> I successfully gather JSON from VideoUnit and QuestionUnit (thanks to plaes) 
> but now I can't figure out why when I try to make the same thing with 
> VideoQuestioUnit the program crash telling me: "KeyError: 'questions'"
> I can imagine that there is something wrong when VideoQuestionUnit try to 
> access the "questions" data declared in QuestionUnit(also my dog get that I 
> know), but I don't understand what's wrong with that. 
> Did someone have an idea of what I'm doing wrong?

first of all, SQLAlchemy’s inheritance mapping system has no direct support for 
multiple inheritance at all.  True multi-table multiple inheritance in SQL is a 
highly esoteric and complex pattern and I doubt SQLAlchemy is ever going to go 
there, at least fully; just getting single hierarchy inheritance to work fully 
has taken many years and was enormously complicated.   While I see here that at 
least VideoQuestionUnit doesn’t have a table of its own, still the fact that it 
is subclassing two other joined-inh subclasses isn’t something the ORM has any 
idea what to do with.   Loading a VideoQuestionUnit means it would need to join 
to both of the inherited tables, like a polymorphic load does, but there’s no 
mechanism to produce one object deriving from more than one mapped class.  I’m 
surprised the mapping doesn’t just fail immediately.

secondly, well actually I’m not sure there’s a “secondly” here :).   I guess 
the way you can load and persist VideoQuestionUnit objects is by mapping it to 
a JOIN of all three tables at once - and you’d also need to probably add 
“concrete=True” to the mapping so that it doesn’t attempt to use any of the 
superclass mappings for persistence or querying.    Not something I’ve tried 
though it might be workable.





> 
> 
> CODE:
> class Unit(db.Model):
>     id = db.Column(db.Integer, unique = True, primary_key = True)
>     name = db.Column(db.String(128), index = True, unique = False)
>     type = db.Column(db.SmallInteger)
>     brand_id = db.Column(db.Integer, db.ForeignKey('brand.id'))
>     @property
>     def serialize(self):
>         print "Serialize on superclass"
>         pass
> 
> class VideoUnit(Unit):
>     uri = db.Column(db.String(256), index = False, unique = False)
>     @property
>     def serialize(self):
>         return {
>             'ID'        :   self.id,
>             'Name'      :   self.name,
>             'Link'      :   self.uri
>         }
> 
> class QuestionUnit(Unit):
>     questions = db.relationship('Question', backref = 'QuestionUnit', lazy = 
> 'dynamic')
>     @property
>     def serialize(self):
>         return {
>             'ID'        :   self.id,
>             'Name'      :   self.name,
>             'Questions' :   self.serialize_questions
>         }
>     @property
>     def serialize_questions(self):
>         return [item.serialize for item in self.questions]
> 
> class VideoQuestionUnit(VideoUnit, QuestionUnit):
>     @property
>     def serialize(self):
>         return {
>             'ID'        :   self.id,
>             'Name'      :   self.name,
>             'Link'      :   self.uri,
>             'Questions' :   self.serialize_questions
>         }
>     @property
>     def serialize_questions(self):
>         return [item.serialize for item in self.questions]
> 
> 
> 
> Thanks in advance,
> 
> Enrico
> 
> -- 
> 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 sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to