The default relationship cascade settings will do it for you. Here I made
them explicit.

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    p_id = Column(Integer, ForeignKey(Parent.id))
    parent = relation(Parent, backref=backref('children',
cascade="save-update,merge"))

sess.add(Parent(children=[Child(),Child()]))
sess.commit()
p = sess.query(Parent).first()
sess.delete(p)
sess.commit()

-- 
Mike Conley



On Fri, Aug 19, 2011 at 12:10 PM, Mark Erbaugh <m...@microenh.com> wrote:

> I have a table that has a foreign key field that is optional.  IOW, the
> current row may be linked to at most one row in the foreign table. If the
> foreign key field is not NULL, it must point to a valid row in the foreign
> table, but if it is NULL that means that it it not linked.
>
> Is there an automatic way to have the value of the foreign key field set to
> NULL if the linked row in the foreign table is deleted?
>
> Thanks,
> Mark
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@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