title: SQLAlchemy: Multiple Inheritance with dynamic 'association_proxy' creator function
body: <p>I am currently trying to create the following database schema with SQLAlchemy (using ext.declarative):</p>
  
  <p>I have a base class <code>MyBaseClass</code> which provides some common functionality for all of my publicly accessible classes, a mixin class <code>MetadataMixin</code> that provides functionality to query metadata from imdb and store it.
  Every class that subclasses <code>MetadataMixin</code> has a field <code>persons</code> which provides a M:N relationship to instances of the <code>Person</code> class, and a field <code>persons_roles</code> which provides a 1:N relationship to an object (one for each subclass) which stores the <code>role</code> a concrete Person plays in the instance of the subclass.</p>
  
  <p>This is an abbreviated version of what my code looks like at the moment:</p>
  
  <pre><code>from sqlalchemy import Column, Integer, Enum, ForeignKey
  from sqlalchemy.orm import relationship
  from sqlalchemy.ext.associationproxy import association_proxy
  from sqlalchemy.ext.declarative import declarative_base
  
  Base = declarative_base()
  
  
  class MyBaseClass(object):
      """Base class for all publicly accessible classes"""
      id = Column(Integer, primary_key=True)
  
  
  class Person(MyBaseClass):
      """A Person"""
  
      name = Column(Unicode)
      movies = association_proxy('movie_roles', 'movie',
                                 creator=lambda m: _PersonMovieRole(movie=m))
      shows = association_proxy('show_roles', 'show',
                                creator=lambda s: _PersonShowRole(show=s=))
  
  
  class _PersonMovieRole(Base):
      """Role for a Person in a Movie"""
      __tablename__ = 'persons_movies'
  
      id = Column(Integer, primary_key=True)
      role = Column(Enum('none', 'actor', 'writer', 'director', 'producer'),
                    default='none')
      person_id = Column(Integer, ForeignKey('persons.id'))
      person = relationship('Person', backref='movie_roles')
      movie_id = Column(Integer, ForeignKey('movies.id'))
      movie = relationship('Movie', backref='persons_roles')
  
  
  class _PersonShowRole(Base):
      """Role for a Person in a Show"""
      __tablename__ = 'persons_shows'
  
      id = Column(Integer, primary_key=True)
      role = Column(Enum('none', 'actor', 'writer', 'director', 'producer'),
                    default='none')
      person_id = Column(Integer, ForeignKey('persons.id'))
      person = relationship('Person', backref='show_roles')
      show_id = Column(Integer, ForeignKey('shows.id'))
      show = relationship('Episode', backref='persons_roles')
  
  
  class MetadataMixin(object):
      """Mixin class that provides metadata-fields and methods"""
  
      # ...
      persons = association_proxy('persons_roles', 'person',
                                  creator= #...???...#)
  
  
  class Movie(Base, MyBaseClass, MetadataMixin):
      #....
      pass
  </code></pre>
  
  <p>What I'm trying to do is to create a generic <code>creator</code> function for <code>association_proxy</code> that creates either a PersonMovieRole or a PersonShowRole object, depending on the class of the concrete instance that a <code>Person</code> is added to. What I'm stuck on at the moment is that I don't know how to pass the calling class to the creator function.
  Is this possible, or is there maybe even an easier way for what I'm trying to accomplish?</p>