Re: [sqlalchemy] Problem with CircularDependencyError
Aleksander Siewierski wrote: > Hi!, > > I have two entities: Channel and Track. Track has a reference to > Channel (channel field - Track is played on this Channel), and Channel > has a reference to Track (last_track field - last planned Track for > Channel). So definition of tables, classes and mappers is: > > """ > import sqlalchemy > from sqlalchemy.orm import * > from sqlalchemy.schema import * > from sqlalchemy.types import * > > sqlalchemy_config = { > 'url' : 'mysql://...', > 'echo': True, > 'pool_recycle': 600, > 'convert_unicode' : True, > } > > ENGINE = sqlalchemy.engine.engine_from_config(sqlalchemy_config, '') > METADATA = sqlalchemy.schema.MetaData() > METADATA.bind = ENGINE > session_maker = sqlalchemy.orm.sessionmaker(autoflush=True, > transactional=True, bind=ENGINE) > SESSION = sqlalchemy.orm.scoped_session(session_maker) > > class Channel(object): > def __init__(self, name): > self.name = name > > class Track(object): > def __init__(self, title): > self.title = title > > channel_t = Table( > 'channels', > METADATA, > Column('id', Integer, primary_key=True), > Column('name', Unicode(256)), > Column('last_track_id', Integer, ForeignKey('tracks.id')), > mysql_engine='InnoDB', > mysql_charset='utf8' > ) > > track_t = Table( > 'tracks', > METADATA, > Column('id', Integer, primary_key=True), > Column('channel_id', Integer, ForeignKey('channels.id'), > nullable=False), > Column('title', Unicode(256)), > mysql_engine='InnoDB', > mysql_charset='utf8' > ) > > mapper(Channel, channel_t, properties = { >'last_track': relation(Track, > primaryjoin=channel_t.c.last_track_id == track_t.c.id), >} > ) > > mapper(Track, track_t, properties={ >'channel': relation(Channel, > primaryjoin=track_t.c.channel_id == channel_t.c.id), >} > ) this pattern is described here: http://www.sqlalchemy.org/docs/mappers.html#rows-that-point-to-themselves-mutually-dependent-rows > > channel1 = SESSION.query(Channel).get(1) > track1 = Track('Track 1') > track1.channel = channel1 > channel1.last_track = track1 > > SESSION.add(track1) > SESSION.commit() > """ > > and when I try to commit this, I have following error: > Traceback (most recent call last): > File "cml.py", line 75, in > SESSION.commit() > File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- > py2.6.egg/sqlalchemy/orm/scoping.py", line 127, in do > return getattr(self.registry(), name)(*args, **kwargs) > File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- > py2.6.egg/sqlalchemy/orm/session.py", line 671, in commit > self.transaction.commit() > File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- > py2.6.egg/sqlalchemy/orm/session.py", line 378, in commit > self._prepare_impl() > File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- > py2.6.egg/sqlalchemy/orm/session.py", line 362, in _prepare_impl > self.session.flush() > File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- > py2.6.egg/sqlalchemy/orm/session.py", line 1354, in flush > self._flush(objects) > File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- > py2.6.egg/sqlalchemy/orm/session.py", line 1432, in _flush > flush_context.execute() > File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- > py2.6.egg/sqlalchemy/orm/unitofwork.py", line 258, in execute > tasks = self._sort_dependencies() > File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- > py2.6.egg/sqlalchemy/orm/unitofwork.py", line 299, in > _sort_dependencies > for t in task._sort_circular_dependencies(self, > [self.get_task_by_mapper(i) for i in cycles]): > File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- > py2.6.egg/sqlalchemy/orm/unitofwork.py", line 582, in > _sort_circular_dependencies > head = topological.sort_as_tree(tuples, > object_to_original_task.iterkeys()) > File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- > py2.6.egg/sqlalchemy/topological.py", line 59, in sort_as_tree > return _organize_as_tree(_sort(tuples, allitems, > allow_cycles=with_cycles)) > File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- > py2.6.egg/sqlalchemy/topological.py", line 209, in _sort > raise CircularDependencyError("Circular dependency detected " + > repr(edges) + repr(queue)) > sqlalchemy.exc.CircularDependencyError: Circular dependency detected > [(, > ), > (, > )][] > > what is wrong with this definitions? Documentation says that > CircularDependencyError should appear only when I do topological > sorting. > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email t
[sqlalchemy] Problem with CircularDependencyError
Hi!, I have two entities: Channel and Track. Track has a reference to Channel (channel field - Track is played on this Channel), and Channel has a reference to Track (last_track field - last planned Track for Channel). So definition of tables, classes and mappers is: """ import sqlalchemy from sqlalchemy.orm import * from sqlalchemy.schema import * from sqlalchemy.types import * sqlalchemy_config = { 'url' : 'mysql://...', 'echo': True, 'pool_recycle': 600, 'convert_unicode' : True, } ENGINE = sqlalchemy.engine.engine_from_config(sqlalchemy_config, '') METADATA = sqlalchemy.schema.MetaData() METADATA.bind = ENGINE session_maker = sqlalchemy.orm.sessionmaker(autoflush=True, transactional=True, bind=ENGINE) SESSION = sqlalchemy.orm.scoped_session(session_maker) class Channel(object): def __init__(self, name): self.name = name class Track(object): def __init__(self, title): self.title = title channel_t = Table( 'channels', METADATA, Column('id', Integer, primary_key=True), Column('name', Unicode(256)), Column('last_track_id', Integer, ForeignKey('tracks.id')), mysql_engine='InnoDB', mysql_charset='utf8' ) track_t = Table( 'tracks', METADATA, Column('id', Integer, primary_key=True), Column('channel_id', Integer, ForeignKey('channels.id'), nullable=False), Column('title', Unicode(256)), mysql_engine='InnoDB', mysql_charset='utf8' ) mapper(Channel, channel_t, properties = { 'last_track': relation(Track, primaryjoin=channel_t.c.last_track_id == track_t.c.id), } ) mapper(Track, track_t, properties={ 'channel': relation(Channel, primaryjoin=track_t.c.channel_id == channel_t.c.id), } ) channel1 = SESSION.query(Channel).get(1) track1 = Track('Track 1') track1.channel = channel1 channel1.last_track = track1 SESSION.add(track1) SESSION.commit() """ and when I try to commit this, I have following error: Traceback (most recent call last): File "cml.py", line 75, in SESSION.commit() File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- py2.6.egg/sqlalchemy/orm/scoping.py", line 127, in do return getattr(self.registry(), name)(*args, **kwargs) File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- py2.6.egg/sqlalchemy/orm/session.py", line 671, in commit self.transaction.commit() File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- py2.6.egg/sqlalchemy/orm/session.py", line 378, in commit self._prepare_impl() File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- py2.6.egg/sqlalchemy/orm/session.py", line 362, in _prepare_impl self.session.flush() File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- py2.6.egg/sqlalchemy/orm/session.py", line 1354, in flush self._flush(objects) File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- py2.6.egg/sqlalchemy/orm/session.py", line 1432, in _flush flush_context.execute() File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- py2.6.egg/sqlalchemy/orm/unitofwork.py", line 258, in execute tasks = self._sort_dependencies() File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- py2.6.egg/sqlalchemy/orm/unitofwork.py", line 299, in _sort_dependencies for t in task._sort_circular_dependencies(self, [self.get_task_by_mapper(i) for i in cycles]): File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- py2.6.egg/sqlalchemy/orm/unitofwork.py", line 582, in _sort_circular_dependencies head = topological.sort_as_tree(tuples, object_to_original_task.iterkeys()) File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- py2.6.egg/sqlalchemy/topological.py", line 59, in sort_as_tree return _organize_as_tree(_sort(tuples, allitems, allow_cycles=with_cycles)) File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.8- py2.6.egg/sqlalchemy/topological.py", line 209, in _sort raise CircularDependencyError("Circular dependency detected " + repr(edges) + repr(queue)) sqlalchemy.exc.CircularDependencyError: Circular dependency detected [(, ), (, )][] what is wrong with this definitions? Documentation says that CircularDependencyError should appear only when I do topological sorting. -- 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.