def _relationships_for_fks(automap_base, map_config, table_to_map_config,
                           collection_class,
                           name_for_scalar_relationship,
                           name_for_collection_relationship,
                           generate_relationship):
    local_table = map_config.local_table
    local_cls = map_config.cls

    if local_table is None:
        return
    for constraint in local_table.constraints:
        if isinstance(constraint, ForeignKeyConstraint):
            fks = constraint.elements
            referred_table = fks[0].column.table
            referred_cfg = table_to_map_config.get(referred_table, None)
            if referred_cfg is None:
                continue
            referred_cls = referred_cfg.cls

            if local_cls is not referred_cls and issubclass(
                    local_cls, referred_cls):
                continue

            relationship_name = name_for_scalar_relationship(
                automap_base,
                local_cls,
                referred_cls, constraint)
            backref_name = name_for_collection_relationship(
                automap_base,
                referred_cls,
                local_cls,
                constraint
            )

            o2m_kws = {}
            nullable = False not in set([fk.parent.nullable for fk in fks])
            if not nullable:
                o2m_kws['cascade'] = "all, delete-orphan"

                if constraint.ondelete and \
                        constraint.ondelete.lower() == "cascade":
                    o2m_kws['passive_deletes'] = True
            else:
                if constraint.ondelete and \
                        constraint.ondelete.lower() == "set null":
                    o2m_kws['passive_deletes'] = True

            create_backref = backref_name not in referred_cfg.properties

            if relationship_name not in map_config.properties:
                if create_backref:
                    backref_obj = generate_relationship(
                        automap_base,
                        interfaces.ONETOMANY, backref,
                        backref_name, referred_cls, local_cls,
                        collection_class=collection_class,
                        **o2m_kws)
                else:
                    backref_obj = None
                rel = generate_relationship(automap_base,
                                            interfaces.MANYTOONE,
                                            relationship,
                                            relationship_name,
                                            local_cls, referred_cls,
                                            foreign_keys=[
                                                fk.parent
                                                for fk in constraint.elements],
                                            backref=backref_obj,
                                            remote_side=[
                                                fk.column
                                                for fk in constraint.elements]
                                            )
                if rel is not None:
                    map_config.properties[relationship_name] = rel
                    if not create_backref:
                        referred_cfg.properties[
                            backref_name].back_populates = relationship_name
            elif create_backref:
                rel = generate_relationship(automap_base,
                                            interfaces.ONETOMANY,
                                            relationship,
                                            backref_name,
                                            referred_cls, local_cls,
                                            foreign_keys=[
                                                fk.parent
                                                for fk in constraint.elements],
                                            back_populates=relationship_name,
                                            collection_class=collection_class,
                                            **o2m_kws)
                if rel is not None:
                    referred_cfg.properties[backref_name] = rel
                    map_config.properties[
                        relationship_name].back_populates = backref_name
dataset/ETHPy150Open zzzeek/sqlalchemy/lib/sqlalchemy/ext/automap.py/_relationships_for_fks/original