Skip to content

fix(models): guard three more backref cascades for SQLAlchemy 2.0 - #42787

Merged
rusackas merged 1 commit into
masterfrom
fix/cascade-backrefs-sqla2-followup
Aug 5, 2026
Merged

fix(models): guard three more backref cascades for SQLAlchemy 2.0#42787
rusackas merged 1 commit into
masterfrom
fix/cascade-backrefs-sqla2-followup

Conversation

@rusackas

@rusackas rusackas commented Aug 5, 2026

Copy link
Copy Markdown
Member

SUMMARY

Discussion #40273 step 2 (SQLAlchemy 2.0 migration roadmap) added cascade_backrefs=False to seven models (Query, SavedQuery, SqlaTable, SqlMetric, TableColumn, TaggedObject, User) whose backref()-defined reverse relationships were triggering SQLAlchemy's "object is being merged into a Session along the backref cascade path" deprecation warning, locked in via a pytest.ini filterwarnings error line per model to prevent regression.

A follow-up sweep for step 6 prep found three more models with the exact same pattern that the original sweep missed: SSHTunnel.database, ReportRecipients.report_schedule, and ReportExecutionLog.report_schedule. Same fix, same reasoning: in SQLAlchemy 2.0 this reverse cascade no longer happens automatically, so callers that relied on it implicitly would silently stop persisting these objects. Verified each caller already reaches the objects via an explicit db.session.add() or the forward collection assignment (which carries its own cascade="all, delete-orphan", unaffected by cascade_backrefs), so nothing here depended on the removed behavior.

TESTING INSTRUCTIONS

SQLALCHEMY_WARN_20=1 pytest tests/unit_tests/databases/ssh_tunnel tests/unit_tests/reports tests/unit_tests/commands/report tests/unit_tests/daos/test_report_dao.py

426 tests pass with the three new pytest.ini error lines active, confirming none of the exercised code paths still rely on the removed implicit cascade.

ADDITIONAL INFORMATION

  • Has associated issue
  • Required feature flags:
  • Changes UI
  • Includes DB Migration
  • Introduces new feature or API
  • Removes existing feature or API

SSHTunnel.database, ReportRecipients.report_schedule, and
ReportExecutionLog.report_schedule were missed by the earlier sweep that
added cascade_backrefs=False to Query, SavedQuery, SqlaTable, SqlMetric,
TableColumn, TaggedObject, and User (discussion #40273, step 2) -- same
reverse-cascade warning, same fix. Locks each in via a pytest.ini
filterwarnings error line, matching the existing entries for the other
seven models.
@bito-code-review

bito-code-review Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #b39056

Actionable Suggestions - 0
Review Details
  • Files reviewed - 3 · Commit Range: f13580d..f13580d
    • pytest.ini
    • superset/databases/ssh_tunnel/models.py
    • superset/reports/models.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@codecov

codecov Bot commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.79%. Comparing base (1605676) to head (f13580d).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #42787   +/-   ##
=======================================
  Coverage   65.79%   65.79%           
=======================================
  Files        2842     2842           
  Lines      162106   162106           
  Branches    37148    37148           
=======================================
  Hits       106653   106653           
  Misses      53388    53388           
  Partials     2065     2065           
Flag Coverage Δ
hive 38.08% <ø> (ø)
mysql 57.90% <ø> (ø)
postgres 57.95% <ø> (ø)
presto 40.00% <ø> (ø)
python 59.32% <ø> (ø)
sqlite 57.57% <ø> (ø)
unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rusackas
rusackas requested review from betodealmeida, sadpandajoe and villebro and a lite review from Copilot August 5, 2026 16:38
@rusackas

rusackas commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

CC @hy144328

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues the SQLAlchemy 2.0 migration work by preventing deprecated “backref cascade” session merges on three additional ORM relationships, aligning runtime behavior with SQLAlchemy 2.0 while locking regressions via pytest.ini warning-as-error rules.

Changes:

  • Disable backref cascade merging for ReportRecipients.report_schedule and ReportExecutionLog.report_schedule by setting cascade_backrefs=False on the ReportSchedule backrefs.
  • Disable backref cascade merging for SSHTunnel.database by setting cascade_backrefs=False on the Database.ssh_tunnel backref.
  • Add pytest.ini filterwarnings error entries for the three models to ensure these warnings cannot regress silently.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
superset/reports/models.py Adds cascade_backrefs=False to ReportSchedule.recipients and ReportSchedule.logs backrefs to prevent SQLAlchemy 2.0 backref-cascade session merge behavior.
superset/databases/ssh_tunnel/models.py Adds cascade_backrefs=False to the Database.ssh_tunnel backref to prevent backref-cascade session merge behavior.
pytest.ini Adds warning-as-error filters for the three newly covered “merged into a Session” RemovedIn20Warning messages.

@villebro villebro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving with some food for thought.

Comment on lines +58 to +60
# SQLAlchemy 2.0 behavior: assigning `ssh_tunnel.database` no
# longer cascades the SSHTunnel into the Database's session;
# callers must add objects to a session explicitly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just make sure the code works on 1.4 and 2.0 and then remove these comments? I assume it just requires adding the child object into the session explicitly wherever that's being done.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already checked that part, ran the suite with SQLALCHEMY_WARN_20=1 and the new pytest.ini lines as hard errors, all 426 pass. So none of the three actually lean on the old implicit cascade, they already either call session.add() explicitly or go through the forward collection assignment, which has its own real cascade untouched by this flag.

On the comments themselves, I'd lean toward keeping them just to match the other seven models that got the identical treatment earlier in this series. Not a hill I need to die on though, curious what you think. @hy144328 might have an opinion too since he's driven most of this convention.

@bito-code-review

Copy link
Copy Markdown
Contributor

The comments were added to document the transition to SQLAlchemy 2.0 behavior, where cascade_backrefs=False is used to prevent automatic session cascading. To remove these comments, you must ensure that all code paths creating or assigning these child objects (SSHTunnel, ReportRecipients, and ReportExecutionLog) explicitly add them to the session (e.g., session.add(obj)). Once you have verified that all such locations in the codebase are updated to handle session management explicitly, it is safe to remove these explanatory comments.

superset/databases/ssh_tunnel/models.py

# SQLAlchemy 2.0 behavior: assigning `ssh_tunnel.database` no
            # longer cascades the SSHTunnel into the Database's session;
            # callers must add objects to a session explicitly.
            cascade_backrefs=False,

@rusackas
rusackas merged commit 05193ed into master Aug 5, 2026
74 checks passed
@rusackas
rusackas deleted the fix/cascade-backrefs-sqla2-followup branch August 5, 2026 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants