, . .
1. delete()
. :
, SQL, mapper
SQL Engineer, ON DELETE CASCADE .
, , , .
, . :
class Location(Base):
__tablename__ = 'location'
id = Column(INTEGER, primary_key=True)
name = Column(VARCHAR(30))
type = Column(VARCHAR(30))
__mapper_args__ = {
'polymorphic_identity': 'location',
'polymorphic_on' : type,
}
class Field(Location):
__tablename__ = 'field'
id = Column(INTEGER, ForeignKey('location.id', ondelete='cascade'), primary_key=True)
size = Column(DECIMAL(20, 2))
__mapper_args__ = {
'polymorphic_identity': 'field',
}
2. , Location
, Field
.
session.query(Location).filter(Location.id == 1).delete()
3. Field
Location
.
session.query(Field).filter(Field.size < 5).delete()
Field
Location
. Field
, .
, Location
Field.size < 5
.
session.query(Location).filter(Field.size < 5).delete()
session.query(Location).outerjoin(Field, Location.id == Field.id).filter(Field.size < 5).delete()
.
:
statment = delete(Field, prefixes=[Location.__tablename__]).where(Field.size == 1)
session.execute(statment)
sql DELETE location FROM location JOIN field ON location.id = field.id WHERE field.size < 5