OK, my last post I tried to edit the code while I was posting to simplify 
it, and clearly missed a bit. Here's the case that is failing simplified as 
much as possible. I get a key error on the last line. I don't have sqlite 
installed, so I skipped actually creating the engine, but I'm not sure that 
matters. Feel free to correct me if I'm wrong.

from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()


class Person(Base):
    __tablename__ = 'Person'
    id = Column(Integer, primary_key=True)
    first_name = Column(String(80))
    last_name = Column(String(120))
    events = association_proxy('person_events','Event')


class PersonEvent(Base):
    __tablename__ = 'PersonEvent';
    person_id = Column(Integer, ForeignKey('Person.id'), primary_key=True)
    event_id = Column(Integer, ForeignKey('Event.id'), primary_key = True)
    role = Column(String(40))


    # bi-directional attribute / collection of "Person" / "Event"
    person = relationship(Person,
                          backref=backref('person_events',
                                          cascade="all, delete-orphan"))
    
    # reference to the Event object
    event = relationship('Event')
    def __init__(self, person=None, event=None, role=None):
        self.person = person
        self.event = event
        self.role = role


class Event(Base):
    __tablename__ = 'Event'
    id = Column(Integer, primary_key=True)
    short_name = Column(String(40))


p1 = Person(first_name='Eric', last_name='Wittle')
p1.events.append(Event(short_name='Birth'))

I thought I was fairly faithfully following the example in Simplifying 
Association Proxies. I'm assuming the first argument to assocation_proxy is 
the name of the backref created in the relationship in the association 
object, but I'm unclear on that. I tried your code suggestion (minus 
actually creating the engine and adding in the __init__ method to the 
association object to work around __init__ takes 1 positional argument but 
two were given error). The edited version I ran is below, and it also gives 
KeyError:

from sqlalchemy import * 
from sqlalchemy.orm import * 
from sqlalchemy.ext.declarative import declarative_base 
import datetime 
from sqlalchemy.ext.associationproxy import association_proxy 

Base = declarative_base() 

class Person(Base): 
    __tablename__ = 'Person' 
    __table_args__ = {'mysql_charset':'utf8'} 
    id = Column(Integer, primary_key=True) 
    full_name = Column(String(240)) 
    email = Column(String(120),unique=True) 
    other_data = Column(String(50)) 
    events = association_proxy('PersonEvent','Event') 

class PersonEvent(Base): 
    __tablename__ = 'PersonEvent'
    __tableargs__ = {'mysql_charset':'utf8'} 
    person_id = Column(Integer, ForeignKey('Person.id'), primary_key=True) 
    event_id = Column(Integer, ForeignKey('Event.id'), primary_key = True) 
    role = Column(String(40)) 

     # bi-directional attribute / collection of "Person" / "Event" 
    person = relationship('Person', 
                          backref=backref("PersonEvent", 
                                          cascade="all, delete-orphan")) 

    # reference to the Event object 
    event = relationship('Event') 

    def __init__(self, person=None, event=None, role=None):
        self.person = person
        self.event = event
        self.role = role

class Event(Base): 
    __tablename__ = 'Event' 
    __table_args__ = {'mysql_charset':'utf8'} 
    id = Column(Integer, primary_key=True) 
    short_name = Column(String(40)) 
    name = Column(String(240)) 
    other_data = Column(String(100)) 

p1 = Person(id=1, full_name='Eric L. Wittle', email='er...@wittle.net') 
e1 = Event(id=1, name='Birth') 
p1.events.append(e1) 

This is running python 3.5.1 with SQLAlchemy 1.1.0b2.

-Eric














-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to