title: SQLAlchemy throwing KeyError when using Association Objects with back_populates – example from documentation doesn't work
body: <p>SQLAlchemy nicely documents <a href="http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#association-object" rel="nofollow noreferrer">how to use Association Objects with <code>back_populates</code></a>.</p>
  
  <p>However, when copy-and-pasting the example from that documentation, adding children to a parent throws a <code>KeyError</code> as following code shows. The model classes are copied 100% from the documentation:</p>
  
  <pre class="lang-python prettyprint-override"><code>from sqlalchemy import Column, ForeignKey, Integer, String
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy.orm import relationship
  from sqlalchemy.schema import MetaData
  
  Base = declarative_base(metadata=MetaData())
  
  class Association(Base):
      __tablename__ = 'association'
      left_id = Column(Integer, ForeignKey('left.id'), primary_key=True)
      right_id = Column(Integer, ForeignKey('right.id'), primary_key=True)
      extra_data = Column(String(50))
      child = relationship("Child", back_populates="parents")
      parent = relationship("Parent", back_populates="children")
  
  class Parent(Base):
      __tablename__ = 'left'
      id = Column(Integer, primary_key=True)
      children = relationship("Association", back_populates="parent")
  
  class Child(Base):
      __tablename__ = 'right'
      id = Column(Integer, primary_key=True)
      parents = relationship("Association", back_populates="child")
  
  parent = Parent(children=[Child()])
  </code></pre>
  
  <p>Running that code with SQLAlchemy version 1.2.11 throws this exception:</p>
  
  <pre><code>lars$ venv/bin/python test.py
  Traceback (most recent call last):
    File "test.py", line 26, in &lt;module&gt;
      parent = Parent(children=[Child()])
    File "&lt;string&gt;", line 4, in __init__
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/orm/state.py", line 417, in _initialize_instance
      manager.dispatch.init_failure(self, args, kwargs)
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
      compat.reraise(exc_type, exc_value, exc_tb)
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 249, in reraise
      raise value
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/orm/state.py", line 414, in _initialize_instance
      return manager.original_init(*mixed[1:], **kwargs)
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/ext/declarative/base.py", line 737, in _declarative_constructor
      setattr(self, k, kwargs[k])
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/orm/attributes.py", line 229, in __set__
      instance_dict(instance), value, None)
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/orm/attributes.py", line 1077, in set
      initiator=evt)
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/orm/collections.py", line 762, in bulk_replace
      appender(member, _sa_initiator=initiator)
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/orm/collections.py", line 1044, in append
      item = __set(self, item, _sa_initiator)
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/orm/collections.py", line 1016, in __set
      item = executor.fire_append_event(item, _sa_initiator)
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/orm/collections.py", line 680, in fire_append_event
      item, initiator)
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/orm/attributes.py", line 943, in fire_append_event
      state, value, initiator or self._append_token)
    File "/Users/lars/coding/sqlalchemy_association_object_test/venv/lib/python3.7/site-packages/sqlalchemy/orm/attributes.py", line 1210, in emit_backref_from_collection_append_event
      child_impl = child_state.manager[key].impl
  KeyError: 'parent'
  </code></pre>
  
  <p>I've filed this as a <a href="https://bitbucket.org/zzzeek/sqlalchemy/issues/4329/keyerror-when-using-association-objects" rel="nofollow noreferrer">bug in SQLAlchemy's issue tracker</a>. Maybe somebody can point me to a working solution or workaround in the meanwhile?</p>