Note that there are some explanatory texts on larger screens.

plurals
  1. POAttribute as dict of lists using middleman table with SQLAlchemy
    primarykey
    data
    text
    <p>My question is about SQLAlchemy but I'm having troubles explaining it in words so I figured I explain it with a simple example of what I'm trying to achieve:</p> <pre><code>parent = Table('parent', metadata, Column('parent_id', Integer, primary_key=True), Column('name', Unicode), ) parent_child = Table('parent_child', metadata, Column('parent_id', Integer, primary_key=True), Column('child_id', Integer, primary_key=True), Column('number', Integer), ForeignKeyConstraint(['parent_id'], ['parent.parent_id']), ForeignKeyConstraint(['child_id'], ['child.child_id']), ) child = Table('child', metadata, Column('child_id', Integer, primary_key=True), Column('name', Unicode), ) class Parent(object): pass class ParentChild(object): pass class Child(object): pass &gt;&gt;&gt; p = Parent(name=u'A') &gt;&gt;&gt; print p.children {} &gt;&gt;&gt; p.children[0] = Child(name=u'Child A') &gt;&gt;&gt; p.children[10] = Child(name=u'Child B') &gt;&gt;&gt; p.children[10] = Child(name=u'Child C') </code></pre> <p>This code would create 3 rows in parent_child table, column number would be 0 for the first row, and 10 for the second and third row.</p> <pre><code>&gt;&gt;&gt; print p.children {0: [&lt;Child A&gt;], 10: [&lt;Child B&gt;, &lt;Child C&gt;]} &gt;&gt;&gt; print p.children[10][0] &lt;Child B&gt; </code></pre> <p>(I left out all SQLAlchemy session/engine code in the example to make it as clean as possible)</p> <p>I did a try using´</p> <pre><code>collection_class=attribute_mapped_collection('number') </code></pre> <p>on a relation between Parent and ParentChild, but it only gave me one child for each number. Not a dict with lists in it. Any help appreciated!</p> <h3>UPDATED!</h3> <p>Thanks to Denis Otkidach I now has this code, but it still doesn't work.</p> <pre><code>def _create_children(number, child): return ParentChild(parent=None, child=child, number=number) class Parent(object): children = association_proxy('_children', 'child', creator=_create_children) class MyMappedCollection(MappedCollection): def __init__(self): keyfunc = lambda attr_name: operator.attrgetter('number') MappedCollection.__init__(self, keyfunc=keyfunc) @collection.appender @collection.internally_instrumented def set(self, value, _sa_initiator=None): key = self.keyfunc(value) try: self.__getitem__(key).append(value) except KeyError: self.__setitem__(key, [value]) mapper(Parent, parent, properties={ '_children': relation(ParentChild, collection_class=MyMappedCollection), }) </code></pre> <p>To insert an Child seems to work</p> <pre><code>p.children[100] = Child(...) </code></pre> <p>But when I try to print children like this:</p> <pre><code>print p.children </code></pre> <p>I get this error:</p> <pre><code>sqlalchemy.orm.exc.UnmappedInstanceError: Class '__builtin__.list' is not mapped </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload