Re: [sqlalchemy] one to many relation, removing and adding list issue

2010-08-26 Thread Michael Bayer

On Aug 25, 2010, at 7:02 AM, Martin-Leon Francois wrote:

 assert(len(o1.to_many) == 2) #this assert fails why?
 

OK so as of r2e09679be06b, your original test now returns:

sqlalchemy.exc.InvalidRequestError: Instance 'Many at 0x12922f0' has been 
deleted.  Use the make_transient() function to send this object back to the 
transient state.


so that it's clear what's going on.

-- 
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.



[sqlalchemy] one to many relation, removing and adding list issue

2010-08-25 Thread Martin-Leon Francois
Hi,

I am trying in the same  session to detach an instance from a collection ( one 
to many)
flush and commit everything (all is ok) and then attach the removed instance  
again. unsuccessfully.

in below code,  last assert fails. 
I don't understand why I am not able to append m2 to o1.to_many collection once 
removed.

Any idea? ( I use sa 0.5.6)

thanx, Francois

meta = MetaData()
tb_one = Table(one, meta,
   Column('name',String(50)),
   Column('id',Integer, primary_key=True))

tb_many = Table(many, meta,
   Column('name',String(50)),
   Column('id',Integer, primary_key=True),
   Column('one_id', Integer, 
ForeignKey(tb_one.c.id,ondelete='CASCADE'), nullable=False),)

class One(object):
def __init__(self, name):
self.name = name

class Many(object):
def __init__(self, name):
self.name = name

mapper_one = mapper(One,tb_one)
mapper_many = mapper(Many, tb_many,
 properties = dict(
 to_one = relation(One,uselist=False, 
backref=backref('to_many', cascade=save-update, merge, delete, 
delete-orphan),)))

engine = create_engine()
Session = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)

meta.bind = engine
meta.drop_all(checkfirst=True) 
meta.create_all(checkfirst=True)

s = Session()
m1 = Many(M1)
m2 = Many(M2)
o1 = One(One)
o1.to_many.append(m1)
o1.to_many.append(m2)

s.add_all([m1,m2,o1])
s.flush()
s.commit()
assert(len(o1.to_many) == 2)

o1.to_many.remove(m2)
assert(len(o1.to_many) == 1)
s.flush()
s.commit()
assert(len(o1.to_many) == 1)

o1.to_many.append(m2)
assert(len(o1.to_many) == 2)
s.flush()
s.commit()
assert(len(o1.to_many) == 2) #this assert fails why?

s.close()

-- 
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.