Skip to content

Migrate Model.query to db.session.execute(select(...)) for SQLAlchemy 2.x compatibility #1100

@catinhere

Description

@catinhere

As part of the Flask 3 / SQLAlchemy 2.x upgrade (#1098), Model.query is deprecated in Flask-SQLAlchemy 3.x and will be removed in a future version. Query.get() is also deprecated in SA 2.x in favour of db.session.get(). Both currently still work but emit deprecation warnings.

Scope

~62 production call sites across:

  • compair/api/ — answer.py, assignment.py, classlist.py, comparison.py, gradebook.py, impersonation.py, lti_launch.py, lti_course.py, login.py, report.py, users.py
  • compair/models/ — comparison.py, assignment_grade.py, answer_score.py, and others
  • compair/manage/

~85 test-side usages in compair/tests/api/

Migration pattern

Before

  answers = Answer.query.filter_by(assignment_id=assignment.id).all()

After

  answers = db.session.execute(
      select(Answer).where(Answer.assignment_id == assignment.id)
  ).scalars().all()

with_entities() calls also go away naturally in this sweep — columns move into the select() argument list.

Query.get() specifically

Before

  assignment = Assignment.query.get(assignment_id)

After

  assignment = db.session.get(Assignment, assignment_id)

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions