KeyError when adding objects using an association proxy

929 views
Skip to first unread message

Stephan Hügel

unread,
Jun 18, 2012, 6:45:41 PM6/18/12
to sqlal...@googlegroups.com
I have two objects, which I'm joining using an association proxy:
(doing this using Flask-sqlalchemy, so Base, engine etc are implicit)

class Correspondent(db.Model, GlyphMixin):
   
# PK column and tablename etc. come from the mixin
    name
= db.Column(db.String(100), nullable=False, unique=True)
   
# association proxy
    tablets
= association_proxy('correspondent_tablets', 'tablet')

def __init__(self, name, tablets=None):
   
self.name = name
   
if tablets:
       
self.tablets = tablets


class Tablet(db.Model, GlyphMixin):
   
# PK column and tablename etc. come from the mixin
    area
= db.Column(db.String(100), nullable=False, unique=True)
   
# association proxy
    correspondents
= association_proxy('tablet_correspondents', 'correspondent')

def __init__(self, area, correspondents=None):
   
self.area = area
   
if correspondents:
       
self.correspondents = correspondents


class Tablet_Correspondent(db.Model):

    __tablename__
= "tablet_correspondent"
    tablet_id
= db.Column("tablet_id",
        db
.Integer(), db.ForeignKey("tablet.id"), primary_key=True)
    correspondent_id
= db.Column("correspondent_id",
        db
.Integer(), db.ForeignKey("correspondent.id"), primary_key=True)
   
# relations
    tablet
= db.relationship(
       
"Tablet",
        backref
="tablet_correspondents",
        cascade
="all, delete-orphan",
        single_parent
=True)
    correspondent
= db.relationship(
       
"Correspondent",
        backref
="correspondent_tablets",
        cascade
="all, delete-orphan",
        single_parent
=True)

   
def __init__(self, tablet=None, correspondent=None):
       
self.tablet = tablet
        self.correspondent = correspondent

However, I get a KeyError when I try to append an object (new or existing) to the Tablet.correspondents or Correspondent.tablets collection. I assume I've done something wrong when implementing the relationships and backrefs on the Tablet_Correspondent model – I was following the association proxy examples, but wanted it to be more symmetrical than the User.keywords example, which is where I'm going wrong, I suspect.


 

Michael Bayer

unread,
Jun 18, 2012, 7:14:57 PM6/18/12
to sqlal...@googlegroups.com
At the very least I would need a full stack trace, exactly how you are appending objects, and preferably the means to actually run it.   Nothing is obviously wrong.    Can you distill your failure into a no-dependencies .py file?  For example I don't have GlyphMixin here, the formatting for __init__ seems to be off in this paste, etc, I don't see how you're manipulating the collection, etc.   No engine is needed since this is just an association proxy creation issue.

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/7jc07UQO__AJ.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Stephan Hügel

unread,
Jun 19, 2012, 4:24:27 AM6/19/12
to sqlal...@googlegroups.com
On Tuesday, 19 June 2012 00:14:57 UTC+1, Michael Bayer wrote:
At the very least I would need a full stack trace, exactly how you are appending objects, and preferably the means to actually run it.   Nothing is obviously wrong.    Can you distill your failure into a no-dependencies .py file?  For example I don't have GlyphMixin here, the formatting for __init__ seems to be off in this paste, etc, I don't see how you're manipulating the collection, etc.   No engine is needed since this is just an association proxy creation issue.

Mike,

no-dep python file and stack trace are at https://gist.github.com/174a76acf27ffef1b66d 
In order to reproduce the issue, I query for two objects:
tab = Tablet.query.first()
cor = Correspondent.query.first()

If I do tab.correspondents or cor.tablets I just get back an empty list in both cases (as expected)
If I do tab.correspondents.append(cor), I get the KeyError you see in line 3 of the stack trace.

-- 
steph

Stephan Hügel

unread,
Jun 20, 2012, 10:25:31 AM6/20/12
to sqlal...@googlegroups.com
Mike,

no-dep python file and stack trace are at https://gist.github.com/174a76acf27ffef1b66d 
In order to reproduce the issue, I query for two objects:
tab = Tablet.query.first()
cor = Correspondent.query.first()

If I do tab.correspondents or cor.tablets I just get back an empty list in both cases (as expected)
If I do tab.correspondents.append(cor), I get the KeyError you see in line 3 of the stack trace.

-- 
steph

As has been pointed out here, I wasn't using the creator lambda while attempting to create the objects in the association object. Problem would have been solved by RTFM, as per usual, and everything's working as expected now.

Michael Bayer

unread,
Jun 20, 2012, 10:46:52 AM6/20/12
to sqlal...@googlegroups.com
great, glad my lack of time to get to this ultimately got it solved ;)

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/tK_tpWvbI58J.
Reply all
Reply to author
Forward
0 new messages