Есть запрос в potresql 

SELECT DISTINCT
  users.name,
  orders_task.id,
  users_orders.status_of_homework,
  users_orders.url_of_solution,
  orders_task.name_of_order, 
  topics.name,
  COUNT(tasks.id) AS number_of_tasks
FROM public.orders_task
JOIN public.users_orders
    ON orders_task.id = users_orders.orders_task_id
JOIN public.users
  ON users_orders.users_id = users.id
JOIN public.tasks_order 
  ON tasks_order.orders_task_id = users_orders.orders_task_id 
JOIN public.tasks 
  ON tasks.id = tasks_order.tasks_id
JOIN public.tasks_topics 
  ON tasks.id = tasks_topics.task_id
JOIN public.topics 
  ON tasks_topics.topics_id = topics.id  
JOIN 
    (SELECT *
    FROM public.users_orders
    WHERE users_orders.users_id = 6 ) AS query_from_users_orders
    ON orders_task.id = query_from_users_orders.orders_task_id
WHERE 
    users_orders.is_homework
GROUP BY 
  orders_task.id, 
  users.name, 
  users_orders.status_of_homework, 
  orders_task.name_of_order, 
  topics.name,
  users_orders.url_of_solution;


во flask он выглядит так
        users_orders_subquery = aliased(Users_orders)
        users_orders_for_user = (
        db.session.query(users_orders_subquery.orders_task_id)
        .filter(users_orders_subquery.users_id == 6)
        .subquery()
        )
        
        res = db.session.query(
            Users.name   
Order_tasks.id, 
Users_orders.status_of_homework, 
Users_orders.url_of_solution,
Order_tasks.name_of_order,
Topics.name
        ).join(
        Users_orders, 
        Order_tasks.id == Users_orders.orders_task_id
        ).join(
        Users, 
        Users_orders.users_id == Users.id
        ).join(
        users_orders_for_user, 
        Order_tasks.id == users_orders_for_user.c.orders_task_id
        ).join(
        Order_tasks.tasks
        ).join(Tasks.topics).filter(
        Users_orders.is_homework == True
        ).group_by(
Order_tasks.id, 
Users.name
Users_orders.status_of_homework, 
Order_tasks.name_of_order,
Topics.name,
Users_orders.url_of_solution
        ).distinct().all()


Как выглядит во flask запрос 
SELECT DISTINCT
  query_from_users_orders.name,
  orders_task.id,
  users_orders.status_of_homework,
  users_orders.url_of_solution,
  orders_task.name_of_order, 
  topics.name
FROM public.orders_task
JOIN public.users_orders
    ON orders_task.id = users_orders.orders_task_id
JOIN public.users
  ON users_orders.users_id = users.id
JOIN public.tasks_order 
  ON tasks_order.orders_task_id = users_orders.orders_task_id 
JOIN public.tasks 
  ON tasks.id = tasks_order.tasks_id
JOIN public.tasks_topics 
  ON tasks.id = tasks_topics.task_id
JOIN public.topics 
  ON tasks_topics.topics_id = topics.id  
JOIN 
    (SELECT 
	users.name,
	users.surname,
	users_orders.orders_task_id
FROM 
	public.users_orders
join 
	public.users
on 
	users.id=users_orders.users_id
where 
	 users_orders.is_homework is False) 
	 AS query_from_users_orders
    ON orders_task.id = query_from_users_orders.orders_task_id
WHERE 
    users_orders.is_homework and users_orders.users_id = 23
GROUP BY 
  orders_task.id, 
  query_from_users_orders.name, 
  users_orders.status_of_homework, 
  orders_task.name_of_order, 
  topics.name,
  users_orders.url_of_solution;