I'll also make the comment that while the pattern you're illustrating is very
unusual (cascade turned off, re-adding detached objects), the ORM is not being
consistent in its treatment of "non-included" child items in mutated
collections during flush, in that your append got flushed but the delete
doesn't - there's specific code to that effect, which is also not consistent
against one-to-many. I've added ticket 1973 which, if it proceeds, would
likely be in 0.7.
On Nov 17, 2010, at 2:57 PM, Michael Bayer wrote:
> 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 [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.
--
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.