seems like a bug, do you have a quick test.py or I can just work one up

On Aug 19, 2013, at 11:13 AM, Georges Dubus <[email protected]> wrote:

> Hello
> 
> I'm trying to use orderinglist with association_proxy to have an ordered many 
> to many relation (as advised here : 
> https://groups.google.com/forum/#!topic/sqlalchemy/S4_8PeRBNJw), and I can 
> get it to behave correctly.
> 
> I wrote a minimal piece of code to show the problem : 
> https://gist.github.com/madjar/d6d5aa151bc3e138e2dc.
> 
> Definitions :
> 
> class Video(Base):
>     __tablename__ = 'videos'
>     id = Column(Integer, primary_key=True)
>     name = Column(String)
> 
>     def __repr__(self):
>         return "<Video: {}>".format(self.name)
> 
> 
> class Queue(Base):
>     __tablename__ = 'queues'
>     id = Column(Integer, primary_key=True)
> 
>     _videos = relationship('VideoInQueue', backref='queue',
>                            order_by='VideoInQueue.position',
>                            collection_class=ordering_list('position'))
> 
>     videos = association_proxy('_videos', 'video',
>                                creator=lambda video: 
> VideoInQueue(video=video))
> 
> 
> class VideoInQueue(Base):
>     __tablename__ = 'videoInQueues'
>     id = Column(Integer, primary_key=True)
>     queue_id = Column(Integer, ForeignKey('queues.id'))
>     video_id = Column(Integer, ForeignKey('videos.id'))
>     position = Column(Integer)
> 
>     video = relationship('Video', backref='queues')
> 
> 
> Main :
> 
> v1 = Video(name='v1')
> v2 = Video(name='v2')
> v3 = Video(name='v3')
> v4 = Video(name='v4')
> q = Queue()
> session.add_all([v1,v2,v3,v4,q])
> q.videos.append(v1)
> q.videos.append(v2)
> print('List of videos after append:', q.videos)
> 
> q.videos.append(v3)
> q.videos.insert(0, v4)
> print('List of videos after insert:', q.videos)
> 
> Output :
> 
> List of videos after append: [<Video: v1>, <Video: v2>]
> List of videos after insert: [<Video: v4>]
> 
> Which is not really what I was expecting.
> 
> To have a better idea of what happened, I ran :
> 
> for vq in session.query(VideoInQueue):
>     print(vq.id, vq.video_id, vq.queue_id, vq.position)
> 
> The result is :
> 
> 1 1 None 0
> 2 2 None 0
> 3 3 None 0
> 4 4 1 0
> 
> 
> So, it seems insert sets the queue_id of the other objects to None. I tried 
> to investigate this myself, but I got lost with the events.
> 
> Anyone has an idea what can be the problem ? Is this a bug or a misuse from 
> me ?
> 
> Thanks a lot
> 
> --
> Georges
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to