Thanks Micahel,
What I intend to model is slightly out of convention here,
The child model has a reference back to the parent, and at the same
time a reference back to the current copy of itself.
On the parent's end, it's only supposed to pick up the current copy,
therefore the additional constraint. (it's not meant to be part of the
foreign key between the parent and child)
Like you said, I am slightly cautious about whether it was the right
thing to do in every case.
On Nov 7, 2:23 am, Michael Bayer <[EMAIL PROTECTED]> wrote:
> the (child.c.current_id ==child.c.id) part of the "parent" relation is
> the culprit here, and also its nonsensical. the relation between
> parent and child is strictly based on the FK between those two
> tables. additionally its by definition a many-to-one relation; there
> is only one possible Parent that can be attached to the Child so even
> if the additional criterion *did* make sense, its still wouldnt affect
> the outcome.
>
> Ill look into clarifying sqlalchemy's behavior with regards to the
> error message, its getting more confused than it should be with this
> particular scenario.
>
> if youre looking to enhance the "child" backref part of this, use
> "backref=backref('child', **additional_criterion)".
>
> On Nov 6, 2007, at 3:42 AM, Esceo wrote:
>
>
>
>
>
> > Hi, all
>
> > the followings are the code snippet
>
> > from sqlalchemy import *
> > meta = MetaData('sqlite://')
>
> > parent = Table('parent', meta,
> > Column('id', Integer, primary_key=True),
> > Column('name', Integer)
> > )
>
> > child = Table('child', meta,
> > Column('id', Integer, primary_key=True),
> > Column('current_id', Integer),
> > Column('parent_id', Integer),
> > ForeignKeyConstraint(['parent_id'],['parent.id'],
> > ondelete="CASCADE"),
> > ForeignKeyConstraint(['current_id'],['child.id']), )
>
> > class Parent(object):
> > pass
>
> > class Child(object):
> > pass
>
> > mapper(Parent, parent);
>
> > mapper(Child, child,
> > properties = { 'parent' : relation(Parent,
> > primaryjoin =
> > (child.c.parent_id == parent.c.id) &
> > (child.c.current_id ==
> > child.c.id),
> > backref = "child"),
> > 'current' : relation(Child,
> > primaryjoin = child.c.current_id ==
> > child.c.id)
> > })
>
> > meta.create_all()
>
> > s = create_session()
> > c = Child()
> > c.parent = Parent()
> > s.save(c)
> > s.flush()
> > s.clear()
>
> > running that resulted in
>
> > Traceback (most recent call last):
> > File "C:\powerforce\test_fk_relation.py", line 39, in <module>
> > s.flush()
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\session.py", line 320, in flush
> > self.uow.flush(self, objects)
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\unitofwork.py", line 210, in flush
> > flush_context.execute()
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\unitofwork.py", line 400, in execute
> > UOWExecutor().execute(self, head)
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\unitofwork.py", line 1018, in execute
> > self.execute_save_steps(trans, task)
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\unitofwork.py", line 1035, in
> > execute_save_steps
> > self.execute_dependencies(trans, task, False)
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\unitofwork.py", line 1048, in
> > execute_dependencies
> > self.execute_dependency(trans, dep, False)
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\unitofwork.py", line 1029, in
> > execute_dependency
> > dep.execute(trans, isdelete)
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\unitofwork.py", line 984, in execute
> > self.processor.process_dependencies(self.targettask, [elem.obj for
> > elem in self.targettask.polymorphic_tosave_elements if elem.obj is not
> > None], trans, delete=False)
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\dependency.py", line 275, in
> > process_dependencies
> > self._synchronize(obj, child, None, False, uowcommit)
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\dependency.py", line 310, in _synchronize
> > self.syncrules.execute(source, dest, obj, child, clearkeys)
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\sync.py", line 92, in execute
> > rule.execute(source, dest, obj, child, clearkeys)
> > File "C:\Python25\lib\site-packages\sqlalchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\sync.py", line 135, in execute
> > value = self.source_mapper.get_attr_by_column(source,
> > self.source_column)
> > File "C:\Python25\lib\site-packages\SQLAlchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\mapper.py", line 1017, in get_attr_by_column
> > prop = self._getpropbycolumn(column, raiseerror)
> > File "C:\Python25\lib\site-packages\SQLAlchemy-0.3.11dev_r3181-
> > py2.5.egg\sqlalchemy\orm\mapper.py", line 1007, in _getpropbycolumn
> > raise exceptions.InvalidRequestError("Column '%s.%s' is not
> > available, due to conflicting property '%s':%s" % (column.table.name,
> > column.name, column.key, repr(prop)))
> > sqlalchemy.exceptions.InvalidRequestError: Column 'child.id' is not
> > available, d
> > ue to conflicting property
> > 'id':<sqlalchemy.orm.properties.ColumnProperty object at 0x016FB730>
>
> > If I had renamed the id column on child to '_id', I ended up with a
> > different error no child.id column configured on the maper Parent|
> > parent|
>
> > any clues?
>
> > Thanks in advance
>
> > Lei- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---