The following code inserts a new element, rather than using the one already
in the session

      # db_file IS in the session at this point in my code
      if db_file.ext() in ['.jpg','.jpeg']:
        pic = Picture(db_file)
        pic.physical_media = [db_file]
        db_pic =
session.query(Picture).filter(Picture.physical_media.contains(db_file)).first()
        # I don't think this condition is ever true
        if not db_pic:
          db_pic = pic
          session.add(db_pic)
          print 'added new:', pic.id
        else:
          print 'pic id:', db_pic.id
        db_pic.tags = db_pic.tags + tagobjs
        session.commit()

This code functions as expected

      # db_file IS in the session at this point in my code
      if db_file.ext() in ['.jpg','.jpeg']:
        pic =
session.query(Picture).filter(Picture.physical_media.contains(db_file)).first()
        # I don't think this condition is ever true
        if not pic:
          pic = Picture(db_file)
          pic.physical_media = [db_file]
          print 'automatically added new:', pic.id
          session.add(pic) # to be safe, in case this auto-add functionality
changes in the future
        else:
          print 'pic id:', pic.id
        pic.tags = pic.tags + tagobjs
        session.commit()

It seems that because of the relationship between physical_media and
pictures that pictures are being automatically added to the session. Is that
the desired functionality? I wouldn't think so.


Perhaps the way I tried to do it is "unpythonic" and the average python guy
wouldn't do it that way; this is why I approach it that way:
In ruby's ActiveRecord I would write something like this:
my_obj = MyObject.find_or_create {:conceptual_key1 => val1, :conceptual_key2
=> val2, :conceptual_key3 => val3}
my_obj.save()

This is nice because it allows me to use composite conceptual keys to find
or create an object rather than using the standard (but much faster) "fake"
integer primary key (which really doesn't represent anything anyway, but
makes for faster joins)


AJ ONeal

--

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.


Reply via email to