I cannot thank you enough - this has been driving me crazy.

Your solution works great.  I also worked out that you can get
create_all() to work by adding use_alter=True to the foreignkey
definition

thumbnail_id=Column('thumbnail_id',Integer, ForeignKey('image.id',
use_alter=True, name='thumbnail_id'))

Thanks again, you saved my sanity.
-Ryan

On Sep 26, 3:17 pm, Mike Conley <[email protected]> wrote:
> You need to use argument            post_update=True
> on your thumbnails relation
>
> http://www.sqlalchemy.org/docs/05/mappers.html#rows-that-point-to-the...
>
> <http://www.sqlalchemy.org/docs/05/mappers.html#rows-that-point-to-the...>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 [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
-~----------~----~----~----~------~----~------~--~---

Reply via email to