Add cascade='delete-orphan' to the relation definition for children. cascade='all,delete-orphan' is also a fairly common option.
See the documentation for other options in cascade. http://www.sqlalchemy.org/docs/05/reference/orm/mapping.html#sqlalchemy.orm.relation -- Mike Conley On Wed, Aug 26, 2009 at 11:20 AM, rajasekhar911 <rajasekhar...@gmail.com>wrote: > > Hi > How do i define a delete-orphan using declarative base? > I am using sqlite and SA0.5.5 > I have defined a one to one relation. > class Child(DeclarativeBase): > __tablename__='children' > id=Column(String(50),primary_key=True) > parent_id=Column(String(50),ForeignKey > ('parent.id',onupdate="CASCADE",ondelete="CASCADE")) > name=Column(String(50)) > > class Parent(DeclarativeBase): > __tablename__='parent' > id=Column(String(50),primary_key=True) > name=Column(String(50)) > children=relation('Child', uselist=False) > > when i delete the parent it makes the parent_id None in Child. > > I tried giving ondelete=DELETE according to > > http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/schema.html#sqlalchemy.schema.ForeignKey > "ondelete – Optional string. If set, emit ON DELETE <value> when > issuing DDL for this constraint. Typical values include CASCADE, > DELETE and RESTRICT." > But gave syntax error while trying to create the child table near > DELETE > > I tried making parent_id as primarykey for Child.But that gave the > error Constraint tried to blank out the > PrimaryKey for instance.... > > what am i doing wrong? > thnx in advance. > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---