Matthew R wrote:
> Hello,
>
> Just getting started with association proxy and having trouble making
> it work. The lookups seem to work fine but when I try to add a new
> element to the association, I get "TypeError: __init__() takes exactly
> 1 argument (2 given)". Code & test case below, I've left a bunch of
> columns out for brevity:
>
>
> from sqlalchemy import Column, Integer, String, DateTime, Boolean,
> ForeignKey, Text, Date
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy.orm import relation, backref
> from sqlalchemy.ext.associationproxy import association_proxy
>
> Base = declarative_base()
>
> class Org(Base):
> __tablename__ = 'tOrg'
>
> id = Column(Integer, primary_key=True, name='OrgID')
> name = Column(String(100), name='OrgName')
>
> def __repr__(self):
> return "<Org(%s: '%s')>" % (self.id, self.name,)
>
>
> class News(Base):
> __tablename__ = 'tNews'
>
> id = Column(Integer, primary_key=True, name='NewsID')
> title = Column(String(255), name='NewsTitle')
> body = Column(Text, name='NewsBody')
> author = Column(String(255), name='NewsAuthor')
> is_active = Column(Boolean, name='NewsActive')
> date = Column(Date, name='NewsDate')
> priority = Column(Integer, name='NewsPriority')
>
> orgs = association_proxy('newsorgs', 'org')
>
> def __repr__(self):
> return "<News(%s: '%s')>" % (self.id, self.title,)
>
> class NewsOrg(Base):
> __tablename__ = 'trefNewsOrg'
>
> id = Column(Integer, primary_key=True, name='NewsOrgID')
> news_id = Column(Integer, ForeignKey(News.id), name='NewsID')
> news = relation('News', backref=backref('newsorgs'))
>
> org_id = Column(Integer, ForeignKey(Org.id), name='OrgID')
> org = relation(Org)
>
> def __repr__(self):
> if self.org:
> orgname = self.org.name
> else:
> orgname = 'ALL'
> return "<NewsOrg(%s: '%s', (%s))>" % (self.id,
> self.news.title, orgname,)
>
> def testcase(session):
> myorg = session.query(Org).filter(Org.id==6).one()
> otherorg_news_associations = session.query(NewsOrg).filter
> (NewsOrg.org_id==1).all()
> mystory = otherorg_news_associations[0].news
> mystory.orgs.append(myorg) # <-- TypeError: __init__() takes
> exactly 1 argument (2 given)
> session.commit()
>
The association_proxy is trying to create the NewsOrg object by calling
NewsOrg(myorg). Since your NewsOrg class does not override __init__, the
default declarative __init__ is used, which takes only keyword
arguments. This causes the "TypeError: __init__() takes exactly 1
argument (2 given)" error.
You can fix this by either adding an __init__ method to NewsOrg like this:
def __init__(self, org=None, **kwargs):
super(NewsOrg, self).__init__(**kwargs)
self.org = org
or, preferably, add a creator argument to association_proxy:
orgs = association_proxy('newsorgs', 'org', creator=lambda org:
NewsOrg(org=org))
You can find out more about the 'creator' argument at:
http://www.sqlalchemy.org/docs/05/reference/ext/associationproxy.html#api
-Conor
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---