Raised by topological sorts when a circular dependency is detected. There are two scenarios where this error occurs: * In a Session flush operation, if two objects are mutually dependent on each other, they can not be inserted or deleted via INSERT or DELETE statements alone; a
| 210 | |
| 211 | |
| 212 | class CircularDependencyError(SQLAlchemyError): |
| 213 | """Raised by topological sorts when a circular dependency is detected. |
| 214 | |
| 215 | There are two scenarios where this error occurs: |
| 216 | |
| 217 | * In a Session flush operation, if two objects are mutually dependent |
| 218 | on each other, they can not be inserted or deleted via INSERT or |
| 219 | DELETE statements alone; an UPDATE will be needed to post-associate |
| 220 | or pre-deassociate one of the foreign key constrained values. |
| 221 | The ``post_update`` flag described at :ref:`post_update` can resolve |
| 222 | this cycle. |
| 223 | * In a :attr:`_schema.MetaData.sorted_tables` operation, two |
| 224 | :class:`_schema.ForeignKey` |
| 225 | or :class:`_schema.ForeignKeyConstraint` objects mutually refer to each |
| 226 | other. Apply the ``use_alter=True`` flag to one or both, |
| 227 | see :ref:`use_alter`. |
| 228 | |
| 229 | """ |
| 230 | |
| 231 | def __init__( |
| 232 | self, |
| 233 | message: str, |
| 234 | cycles: Any, |
| 235 | edges: Any, |
| 236 | msg: Optional[str] = None, |
| 237 | code: Optional[str] = None, |
| 238 | ): |
| 239 | if msg is None: |
| 240 | message += " (%s)" % ", ".join(repr(s) for s in cycles) |
| 241 | else: |
| 242 | message = msg |
| 243 | SQLAlchemyError.__init__(self, message, code=code) |
| 244 | self.cycles = cycles |
| 245 | self.edges = edges |
| 246 | |
| 247 | def __reduce__(self) -> Union[str, Tuple[Any, ...]]: |
| 248 | return ( |
| 249 | self.__class__, |
| 250 | (None, self.cycles, self.edges, self.args[0]), |
| 251 | {"code": self.code} if self.code is not None else {}, |
| 252 | ) |
| 253 | |
| 254 | |
| 255 | class CompileError(SQLAlchemyError): |