from models import db, Samples, Users, Annotations from app import app from sqlalchemy import text if __name__ == '__main__': with app.app_context(): # AND annotations.user_id <> '{id_user}' id_user = 4 query = text( f''' SELECT samples.id, text FROM samples LEFT JOIN annotations ON samples.id = annotations.sample_id WHERE samples.id NOT IN ( SELECT sample_id FROM annotations GROUP BY sample_id HAVING COUNT(sample_id) > 5 ) AND samples.id NOT IN ( SELECT samples.id FROM samples LEFT JOIN annotations ON samples.id = annotations.sample_id WHERE annotations.user_id IS {id_user} ) ORDER BY samples.id ASC LIMIT 1; ''' ) # query = text( # ''' # SELECT samples.id # FROM samples # LEFT JOIN annotations # ON samples.id = annotations.sample_id # WHERE annotations.user_id IS NOT 1 # ''' # ) result = db.session.execute(query) print(result.fetchall()) annotations = Annotations.query.all() print(len(annotations)) # for annotation in annotations: # print(annotation.user_id)