You need to use argument            post_update=True
on your thumbnails relation

http://www.sqlalchemy.org/docs/05/mappers.html#rows-that-point-to-themselves-mutually-dependent-rows

<http://www.sqlalchemy.org/docs/05/mappers.html#rows-that-point-to-themselves-mutually-dependent-rows>Here
is a sample I used that seems to work. Interesting is that you cannot create
the tables with meta.create_all() because of te circular dependency. I
created the table in 2 separate calls.

class Image(Base):
    __tablename__='image'
    id = Column(Integer, primary_key=True)
    project_id = Column(Integer, ForeignKey('project.id'))
    def __repr__(self):
        return "<I> id:%s" % self.id

class Project(Base):
    __tablename__='project'
    id = Column(Integer, primary_key=True)
    thumbnail_id = Column(Integer, ForeignKey('image.id') )
    images = relation('Image', backref=backref('project'),
        primaryjoin="Project.id==Image.project_id",
        foreign_keys=[Image.project_id]
        )
    thumbnail = relation(Image,
        primaryjoin="Project.thumbnail_id==Image.id",
        foreign_keys=[thumbnail_id],
        uselist=False, post_update=True)
    def __repr__(self):
        return "<P> id:%s thumb:%s" % (self.id, self.thumbnail_id)

Image.__table__.create()
Project.__table__.create()

P1 = Project()
I1 = Image()
I2 = Image()
I3 = Image()
P1.images.extend([I1,I2,I3])
P1.thumbnail=I2
session.add(P1)
session.flush()

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@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
-~----------~----~----~----~------~----~------~--~---

Reply via email to