[Sqlalchemy-tickets] Issue #4329: KeyError when using Association Objects with back_populates – exam
Brought to you by:
zzzeek
From: Lars B. <iss...@bi...> - 2018-08-30 16:29:38
|
New issue 4329: KeyError when using Association Objects with back_populates – example from documentation doesn't work https://bitbucket.org/zzzeek/sqlalchemy/issues/4329/keyerror-when-using-association-objects Lars Blumberg: [This is a cross post from StackOverflow](https://stackoverflow.com/questions/52101595/sqlalchemy-throwing-keyerror-when-using-association-objects-with-back-populates) SQLAlchemy nicely documents how to use Association Objects with back_populates. However, when copy-and-pasting the example from that documentation, adding children to a parent throws a KeyError as following code shows. The model classes are copied 100% from the documentation: ``` #!python 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()]) ``` Running that code with SQLAlchemy version 1.2.11 throws this exception: ``` lars$ venv/bin/python test.py Traceback (most recent call last): File "test.py", line 26, in <module> parent = Parent(children=[Child()]) File "<string>", 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' ``` |