you've turned off "save-update" cascade so the "c" object is not placed into 
the Session when you do the final save() of p.

Fix:

session.add_all(p.children)
p.children = []

save(session, p)


On Nov 17, 2010, at 12:38 PM, Joril wrote:

> from sqlalchemy import Column, String, Integer, Table, ForeignKey
> from sqlalchemy.engine import create_engine
> from sqlalchemy.orm import sessionmaker, relationship
> from sqlalchemy.ext.declarative import declarative_base
> 
> Base = declarative_base()
> 
> junction = Table("junction", Base.metadata, Column("p_id",
> ForeignKey("parents.id")),
>                                            Column("c_id",
> ForeignKey("children.id")))
> 
> class Child(Base):
>   __tablename__ = "children"
> 
>   id = Column(Integer, primary_key=True)
> 
> class Parent(Base):
>   __tablename__ = "parents"
> 
>   id = Column(Integer, primary_key=True)
>   children = relationship(Child, secondary=junction, cascade="")
> 
> 
> def save(session, x):
>   session.add(x)
>   session.flush()
> 
> en = create_engine("sqlite:///:memory:", echo=True)
> Base.metadata.create_all(en)
> maker = sessionmaker(en)
> session = maker(autocommit=True)
> 
> # Save a child and close the session
> c = Child()
> save(session, c)
> session.close()
> 
> # Associate the child to a parent and save
> p = Parent()
> p.children = [c]
> save(session, p)
> 
> # Try to remove the child
> p.children = []
> save(session, p)
> 

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