Stephan Hügel | 19 Jun 2012 00:45
Picon
Gravatar

KeyError when adding objects using an association proxy

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.


 

--
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 sqlalchemy <at> googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Michael Bayer | 19 Jun 2012 01:14
Gravatar

Re: KeyError when adding objects using an association proxy

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.

On Jun 18, 2012, at 6:45 PM, Stephan Hügel wrote:

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.


 

--
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 sqlalchemy <at> googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlalchemy <at> googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Stephan Hügel | 19 Jun 2012 10:24
Picon
Gravatar

Re: KeyError when adding objects using an association proxy

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

--
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/-/xkVE7NJNbBEJ.
To post to this group, send email to sqlalchemy <at> googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Stephan Hügel | 20 Jun 2012 16:25
Picon
Gravatar

Re: KeyError when adding objects using an association proxy

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.

--
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.
To post to this group, send email to sqlalchemy <at> googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Michael Bayer | 20 Jun 2012 16:46
Gravatar

Re: KeyError when adding objects using an association proxy

great, glad my lack of time to get to this ultimately got it solved ;)

On Jun 20, 2012, at 10:25 AM, Stephan Hügel wrote:

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.

--
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.
To post to this group, send email to sqlalchemy <at> googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

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

Gmane