def _m2m_relationship(automap_base, lcl_m2m, rem_m2m, m2m_const, table,
                      table_to_map_config,
                      collection_class,
                      name_for_scalar_relationship,
                      name_for_collection_relationship,
                      generate_relationship):

    map_config = table_to_map_config.get(lcl_m2m, None)
    referred_cfg = table_to_map_config.get(rem_m2m, None)
    if map_config is None or referred_cfg is None:
        return

    local_cls = map_config.cls
    referred_cls = referred_cfg.cls

    relationship_name = name_for_collection_relationship(
        automap_base,
        local_cls,
        referred_cls, m2m_const[0])
    backref_name = name_for_collection_relationship(
        automap_base,
        referred_cls,
        local_cls,
        m2m_const[1]
    )

    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.MANYTOMANY,
                backref,
                backref_name,
                referred_cls, local_cls,
                collection_class=collection_class
            )
        else:
            backref_obj = None
        rel = generate_relationship(automap_base,
                                    interfaces.MANYTOMANY,
                                    relationship,
                                    relationship_name,
                                    local_cls, referred_cls,
                                    secondary=table,
                                    primaryjoin=and_(
                                        fk.column == fk.parent
                                        for fk in m2m_const[0].elements),
                                    secondaryjoin=and_(
                                        fk.column == fk.parent
                                        for fk in m2m_const[1].elements),
                                    backref=backref_obj,
                                    collection_class=collection_class
                                    )
        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.MANYTOMANY,
                                    relationship,
                                    backref_name,
                                    referred_cls, local_cls,
                                    secondary=table,
                                    primaryjoin=and_(
                                        fk.column == fk.parent
                                        for fk in m2m_const[1].elements),
                                    secondaryjoin=and_(
                                        fk.column == fk.parent
                                        for fk in m2m_const[0].elements),
                                    back_populates=relationship_name,
                                    collection_class=collection_class)
        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/_m2m_relationship/original