of course you'll get a cycle if you do this, though: s1 = School(cod="S1", cod_riferimento="S1", cliente=False) d1 = School(cod="D1", cod_riferimento="S1", cliente=False) s1.sedi = [s1, d1]
s1->s1 is not supported by self referential flushes, unless you put post_update=True on the relation() you have there. On Aug 26, 2010, at 12:30 PM, Michael Bayer wrote: > need a full test case. Here's yours, runs fine: > > from sqlalchemy.ext.declarative import declarative_base > from sqlalchemy import * > from sqlalchemy.types import * > from sqlalchemy.orm import * > > Base = declarative_base() > e = create_engine('sqlite://', echo=True) > Base.metadata.bind = e > > sess = sessionmaker(e)() > > class School(Base): > __tablename__ = 'scuola_scuola' > > cod = Column(String(10), primary_key=True) > denominazione= Column(String(120)) > cod_riferimento = Column(String(10), ForeignKey(cod)) > cliente = Column(Boolean, nullable=False) > > sedi = relation('School', ) > > def __repr__(self): > return self.cod > > Base.metadata.create_all() > > s1 = School(cod="S1", cod_riferimento="S1", cliente=False) > d1 = School(cod="D1", cod_riferimento="S1", cliente=False) > sess.add(s1) > sess.add(d1) > sess.commit() > s1 = sess.query(School).get('S1') > d1 = sess.query(School).get('D1') > d1.cliente = False > sess.commit() > > > On Aug 26, 2010, at 12:12 PM, Alessandro Dentella wrote: > >> Hi again, sorry for flooding with email this week... >> >> I stumbled on the CircularDependencyError in some occasions with self >> referencing models. I do understand that it can be tricky to INSERT and >> DELETE >> but I'm just updating rows. >> >> I reduced my problem to the bare minimum. It works both on 0.5.8 and 0.6.3 >> in the test case, but as long as I use the same objects from a graphical GUI >> I get an error, Debugging with pdb, the problem arises on session.begin() >> and in that moment the situation seems to me exactly the one of the test (2 >> objects, one modified). >> >> I hope the error message is meaningful to you... >> >> So the situation: >> >> from sqlalchemy.ext.declarative import declarative_base >> from sqlalchemy import Table, Column, ForeignKey >> from sqlalchemy.types import * >> from sqlalchemy import orm, sql >> >> Base = declarative_base() >> URL = 'postgresql://localhost/scuole' >> Base.metadata.bind = URL >> Session = orm.sessionmaker() >> sess = Session(bind=Base.metadata.bind, expire_on_commit=False, >> autoflush=False, autocommit=True, ) >> >> class School(Base): >> __tablename__ = 'scuola_scuola' >> >> cod = Column(String(10), primary_key=True) >> denominazione= Column(String(120)) >> cod_riferimento = Column(String(10), ForeignKey(cod)) >> cliente = Column(Boolean, nullable=False) >> >> sedi = orm.relation('School', ) >> >> def __repr__(self): >> return self.cod >> >> Base.metadata.create_all() >> >> # s1 = School(cod="S1", cod_riferimento="S1", cliente=False) >> # d1 = School(cod="D1", cod_riferimento="S1", cliente=False) >> # sess.add(s1) >> # sess.add(d1) >> # sess.commit() >> s1 = sess.query(School).get('S1') >> d1 = sess.query(School).get('D1') >> d1.cliente = False >> sess.begin() >> sess.commit() >> >> This same peace of code (i.e.: same session with just s1, d1), run from >> within a GUI raises an error (only with SA 0.6.3, 0.5.8 just works). The >> error is: >> >> >> >> Traceback (most recent call last): >> File "/misc/src/hg/py/sqlkit/sqlkit/widgets/mask/mask.py", line 388, in >> record_save_cb >> self.record_save(None) >> File "/misc/src/hg/py/sqlkit/sqlkit/widgets/mask/mask.py", line 422, in >> record_save >> self.commit() >> File "/misc/src/hg/py/sqlkit/sqlkit/widgets/common/sqlwidget.py", line 972, >> in commit >> self.session.begin() >> File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py", line >> 598, in begin >> self, nested=nested) >> File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py", line >> 223, in __init__ >> self._take_snapshot() >> File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py", line >> 271, in _take_snapshot >> self.session.flush() >> File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py", line >> 1346, in flush >> self._flush(objects) >> File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py", line >> 1427, in _flush >> flush_context.execute() >> File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py", >> line 291, in execute >> postsort_actions): >> File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/topological.py", line >> 31, in sort_as_subsets >> (find_cycles(tuples, allitems), _dump_edges(edges, True))) >> >> sqlalchemy.exc.CircularDependencyError: Circular dependency detected: >> cycles: set([SaveUpdateState(<School at 0x8da616c>), >> ProcessState(OneToManyDP(School.sedi), <School at 0x8da616c>, >> delete=False)]) all edges: [(SaveUpdateState(<School at 0x8da616c>), >> ProcessState(OneToManyDP(School.sedi), <School at 0x8da616c>, >> delete=False)), (ProcessState(OneToManyDP(School.sedi), <School at >> 0x8da616c>, delete=False), SaveUpdateState(<School at 0x8da616c>)), >> (SaveUpdateState(<School at 0x8da616c>), SaveUpdateState(<School at >> 0x8da616c>)), (SaveUpdateState(<School at 0x8da616c>), >> SaveUpdateState(<School at 0x8da61ec>)), >> (ProcessState(OneToManyDP(School.sedi), <School at 0x8da616c>, >> delete=False), SaveUpdateState(<School at 0x8da61ec>))] >> >> How can I further investigate what Is wrong from the setup of my GUI? >> >> Thanks again >> sandro >> *:-) >> >> >> -- >> Sandro Dentella *:-) >> http://sqlkit.argolinux.org SQLkit home page - PyGTK/python/sqlalchemy >> >> -- >> You received this message because you are subscribed to the Google Groups >> "sqlalchemy" group. >> To post to this group, send email to sqlalch...@googlegroups.com. >> To unsubscribe from this group, send email to >> sqlalchemy+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/sqlalchemy?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalch...@googlegroups.com. > To unsubscribe from this group, send email to > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.