I have a table with several dependent tables that I want cascade delete. I'm having problems with it cascading too far. Some code will help explain.
class Map(Base):
....
#One to many relationship between the Map and Tile.
#Each Map is made up of many tiles
tiles = relationship('Tile', lazy='joined', backref='map',
cascade="all, delete")
class Tile(Base):
....
#Reference to Map class.
map_id = Column(Integer,
ForeignKey('maps.id'),
nullable=False)
#Reference to graphics for this tile
#This is a many to one relationship, each Graphic is used by many Tiles
graphics_id = Column(Integer,
ForeignKey("graphics.id"),
nullable=False)
graphics = relationship("Graphic", uselist=False)
class Graphic(Base):
....
#Nothing special here
The problem is that when I delete Map Graphics is also deleted which is not what I want. I assume this is to do with cascade.
How do I fix this so that deleting the Map class will delete the Tiles but not the Graphics?