code: def prepare_relationships(db, known_tables):
      
      inspector = reflection.Inspector.from_engine(db.engine)
      for cls in set(known_tables.values()):
          for foreign_key in inspector.get_foreign_keys(cls.__tablename__):
              if foreign_key['referred_table'] in known_tables:
                  other = known_tables[foreign_key['referred_table']]
                  constrained_column = foreign_key['constrained_columns']
                  if other not in cls.__related_tables__ and cls not in (
                          other.__related_tables__) and other != cls:
                      cls.__related_tables__.add(other)
                      # Add a SQLAlchemy relationship as an attribute
                      # on the class
                      setattr(cls, other.__table__.name, relationship(
                              other.__name__, backref=db.backref(
                                  cls.__name__.lower()),
                              foreign_keys=str(cls.__name__) + '.' +
                              ''.join(constrained_column)))
docs: Enrich the registered Models with SQLAlchemy ``relationships``
      so that related tables are correctly processed up by the admin.