Skip to content

Commit

Permalink
Fx nasty bug where related objects could get added to the session eve…
Browse files Browse the repository at this point in the history
…n though they were never requests.

Backrefs, yay...
  • Loading branch information
cjw296 committed Jun 16, 2016
1 parent 55f55e4 commit 0eba144
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion chide/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, mapping):
def _attrs(self, type_, attrs, nest):
computed_attrs = dict(self.mapping[type_])
for key, value in computed_attrs.items():
if value in self.mapping:
if value in self.mapping and key not in attrs:
computed_attrs[key] = nest(value)
computed_attrs.update(attrs)
return computed_attrs
Expand Down
34 changes: 34 additions & 0 deletions tests/test_sqlalchemy_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,37 @@ def test_closed_session_with_set(self):

obj2 = collection.make(Model)
compare(obj2.value, expected='two')

def test_relationship_with_backrefs(self):
Base = declarative_base()

class Bar(Base):
__tablename__ = 'bar'
id = Column(Integer, primary_key=True)
value = Column(Integer)

class Foo(Base):
__tablename__ = 'foo'
id = Column(Integer, primary_key=True)
bar_id = Column(String, ForeignKey('bar.id'))
bar = relationship('Bar', backref='foos')

class Baz(Base):
__tablename__ = 'baz'
id = Column(Integer, primary_key=True)
foo_id = Column(String, ForeignKey('foo.id'))
foo = relationship('Foo')

collection = Collection({
Bar: {'id': 1, 'value': 2},
Foo: {'id': 3, 'bar': Bar, 'bar_id': 1},
Baz: {'id': 4, 'foo': Foo, 'foo_id': 3}
})
samples = Set(collection)

session = self.make_session(Base)

session.add(samples.get(Baz, id=5, foo=samples.get(Foo, id=7)))
session.commit()

compare(session.query(Foo.id).all(), expected=[(7, )])

0 comments on commit 0eba144

Please sign in to comment.