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()
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---