(
automap_base: Type[Any],
map_config: _DeferredDeclarativeConfig,
table_to_map_config: Union[
Dict[Optional[Table], _DeferredDeclarativeConfig],
Dict[Table, _DeferredDeclarativeConfig],
],
collection_class: type,
name_for_scalar_relationship: NameForScalarRelationshipType,
name_for_collection_relationship: NameForCollectionRelationshipType,
generate_relationship: GenerateRelationshipType,
)
| 1490 | |
| 1491 | |
| 1492 | def _relationships_for_fks( |
| 1493 | automap_base: Type[Any], |
| 1494 | map_config: _DeferredDeclarativeConfig, |
| 1495 | table_to_map_config: Union[ |
| 1496 | Dict[Optional[Table], _DeferredDeclarativeConfig], |
| 1497 | Dict[Table, _DeferredDeclarativeConfig], |
| 1498 | ], |
| 1499 | collection_class: type, |
| 1500 | name_for_scalar_relationship: NameForScalarRelationshipType, |
| 1501 | name_for_collection_relationship: NameForCollectionRelationshipType, |
| 1502 | generate_relationship: GenerateRelationshipType, |
| 1503 | ) -> None: |
| 1504 | local_table = cast("Optional[Table]", map_config.local_table) |
| 1505 | local_cls = cast( |
| 1506 | "Optional[Type[Any]]", map_config.cls |
| 1507 | ) # derived from a weakref, may be None |
| 1508 | |
| 1509 | if local_table is None or local_cls is None: |
| 1510 | return |
| 1511 | for constraint in local_table.constraints: |
| 1512 | if isinstance(constraint, ForeignKeyConstraint): |
| 1513 | fks = constraint.elements |
| 1514 | referred_table = fks[0].column.table |
| 1515 | referred_cfg = table_to_map_config.get(referred_table, None) |
| 1516 | if referred_cfg is None: |
| 1517 | continue |
| 1518 | referred_cls = referred_cfg.cls |
| 1519 | |
| 1520 | if local_cls is not referred_cls and issubclass( |
| 1521 | local_cls, referred_cls |
| 1522 | ): |
| 1523 | continue |
| 1524 | |
| 1525 | relationship_name = name_for_scalar_relationship( |
| 1526 | automap_base, local_cls, referred_cls, constraint |
| 1527 | ) |
| 1528 | backref_name = name_for_collection_relationship( |
| 1529 | automap_base, referred_cls, local_cls, constraint |
| 1530 | ) |
| 1531 | |
| 1532 | o2m_kws: Dict[str, Union[str, bool]] = {} |
| 1533 | nullable = False not in {fk.parent.nullable for fk in fks} |
| 1534 | if not nullable: |
| 1535 | o2m_kws["cascade"] = "all, delete-orphan" |
| 1536 | |
| 1537 | if ( |
| 1538 | constraint.ondelete |
| 1539 | and constraint.ondelete.lower() == "cascade" |
| 1540 | ): |
| 1541 | o2m_kws["passive_deletes"] = True |
| 1542 | else: |
| 1543 | if ( |
| 1544 | constraint.ondelete |
| 1545 | and constraint.ondelete.lower() == "set null" |
| 1546 | ): |
| 1547 | o2m_kws["passive_deletes"] = True |
| 1548 | |
| 1549 | create_backref = backref_name not in referred_cfg.properties |
no test coverage detected