def _sanitize(migrate_engine, table):
 session = orm.sessionmaker(bind=migrate_engine)()
 qry = session.query(table.c.image_id, table.c.member).group_by(table.c.image_id, table.c.member).having((func.count() > 1))
 for (image_id, member) in qry:
  d = table.delete().where(and_((table.c.deleted == True), (table.c.image_id == image_id), (table.c.member == member)))
  d.execute()
 session.close()

def _sanitize ( migrate_engine , table ) : session = orm . sessionmaker ( bind = migrate_engine ) ( ) qry = session . query ( table . c . image_id , table . c . member ) . group_by ( table . c . image_id , table . c . member ) . having ( ( func . count ( ) > 1 ) ) for ( image_id , member ) in qry : d = table . delete ( ) . where ( and_ ( ( table . c . deleted == True ) , ( table . c . image_id == image_id ) , ( table . c . member == member ) ) ) d . execute ( ) session . close ( )
avoid possible integrity error by removing deleted rows to accommdate less restrictive uniqueness constraint .