[Sqlalchemy-tickets] Issue #3583: `obj.foo = set(obj.foo)` (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
|
From: thiefmaster <iss...@bi...> - 2015-11-12 14:52:40
|
New issue 3583: `obj.foo = set(obj.foo)` https://bitbucket.org/zzzeek/sqlalchemy/issues/3583/objfoo-set-objfoo thiefmaster: I have an `association_proxy` on a set-like relationship. Normal set operations work perfectly when it comes to not adding duplicates, but when I replace the whole set e.g. using `foo.bar = set(foo.bar)` (in my real application the right side comes from a form) I get errors due to duplicate rows being inserted. ```python from sqlalchemy import * from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import * Base = declarative_base() class A(Base): __tablename__ = 'test_a' id = Column(Integer, primary_key=True) b_rel = relationship('B', collection_class=set, cascade='all, delete-orphan') b = association_proxy('b_rel', 'value', creator=lambda x: B(value=x)) class B(Base): __tablename__ = 'test_b' __table_args__ = UniqueConstraint('a_id', 'value'), id = Column(Integer, primary_key=True) a_id = Column(Integer, ForeignKey('test_a.id'), nullable=False) value = Column(String) e = create_engine('sqlite:///:memory:', echo=True) # e = create_engine('postgresql:///test', echo=True) Base.metadata.create_all(e) # e.execute('TRUNCATE test_a, test_b;') s = Session(e) a = A() a.b = {'x', 'y', 'z'} s.add(a) s.commit() print print 'adding existing element to set' a.b.add('x') s.flush() print print 'assigning same items to set' a.b = set(a.b) s.flush() ``` |