I'm trying to figure out how to add objects through the ORM.  (My
schema and mappings are below.)

In the shell, I can do the following:
newQ = Question()
# ... set the attributes of newQ
mytype = session.query(QType).first()
mytype.my_sections
# correctly gives all the sections that belong to the type
mytype.my_sections[0].questions
# correctly gives only the questions that belong to both the type and
the section (how does this work btw?)
mytype.my_sections[0].questions.append(newQ)
# inserts only the section_id and question_id into the jointable; it's
missing the type id

How can I get it to also insert the type_id?

Thanks,

Matt


-----------------------------------------------------
questions_table : id | question
sections_table : id | name
types_table : id | name
join_table : type_id | question_id | section_id

class QType(object):
    allquestions = association_proxy('joinObj', 'questions')
class QJoin(object): pass
class Question(object): pass
class Section(object): pass

sections_by_type = select(
    [join_table.c.type_id, join_table.c.section_id],
    group_by=[join_table.c.section_id]).alias('sections_by_type')

mapper(Question, questions_table)
mapper(Section, sections_table, properties={
    'questions'     :relation(Question, secondary=join_table,
        primaryjoin = sections_table.c.id == join_table.c.section_id,
        secondaryjoin = join_table.c.question_id ==
questions_table.c.id,
        backref='section'),
})
mapper(QJoin, join_table, properties={
    'type'          :relation(QType),
    'sections'      :relation(Section, backref='parent'),
    'questions'     :relation(Question, backref='parent'),
})
mapper(QType, types_table, properties={
    'joinObj'       :relation(QJoin),
    'my_sections'   :relation(Section, secondary=sections_by_type,
        primaryjoin = types_table.c.id == sections_by_type.c.type_id,
        backref='type'),
})
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to