On Oct 18, 2011, at 10:03 AM, fribes wrote:

> Hi all,
> 
> Despite some doc and web digging, I didn't find how to tell sqa to behave the 
> way I want :
> on deletion on Peripheral, also delete in Actuator. 
> 
> with the following code, the record in Actuator remains after a deletion, and 
> a subsequent creation fails with IntegrityError.
> 
> class Peripheral(Base):
>     __tablename__ = 'peripheral'
>     id = Column(Integer, primary_key=True)
>     label = Column(String(20), nullable=False)
> 
>     __mapper_args__ = {'polymorphic_on': peripheral_type,
>                        'polymorphic_identity': 'peripheral'}
> 
> class Actuator(Peripheral):
>     __tablename__ = 'actuator'
>     __mapper_args__ = {'polymorphic_identity': 'actuator'}
>     id = Column(None, ForeignKey('peripheral.id'), primary_key=True)
>     
>     duration = Column(Integer)
> 
> Any suggestion ?

usually relationship() with cascade="all, delete-orphan" is used for this use 
case, so that SQLAlchemy can maintain knowledge about the link between 
Peripheral and Actuator.  The other alternative is to use "ON DELETE CASCADE" 
on the foreign key; this is part of the schema you'd generate in the database.

Relevant docs:

http://www.sqlalchemy.org/docs/orm/tutorial.html#configuring-delete-delete-orphan-cascade
http://www.sqlalchemy.org/docs/core/schema.html#on-update-and-on-delete
http://www.sqlalchemy.org/docs/orm/collections.html#using-passive-deletes



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

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