Skip to content

fix(models): correct TabState.latest_query_id column type to match FK target#38837

Merged
aminghadersohi merged 1 commit into
masterfrom
fix-fab-upgrade
Mar 25, 2026
Merged

fix(models): correct TabState.latest_query_id column type to match FK target#38837
aminghadersohi merged 1 commit into
masterfrom
fix-fab-upgrade

Conversation

@sadpandajoe
Copy link
Copy Markdown
Member

@sadpandajoe sadpandajoe commented Mar 25, 2026

User description

SUMMARY

TabState.latest_query_id is defined as Integer in the ORM model, but it references Query.client_id which is String(11). The original migration from 2019 correctly created the column as VARCHAR(11), so existing databases work fine — but the ORM definition has been wrong since the model was first added.

This mismatch is harmless when tables are created via Alembic migrations (which use the correct type). However, any code path that calls Model.metadata.create_all() — such as FAB's SecurityManager.create_db() — will attempt to create the table using the ORM definition and fail with:

psycopg2.errors.DatatypeMismatch: foreign key constraint "tab_state_latest_query_id_fkey" cannot be implemented
DETAIL: Key columns "latest_query_id" and "client_id" are of incompatible types: integer and character varying.

This was exposed by the FAB 5.1.0 → 5.2.0 upgrade in #37973. FAB 5.2.0 added the new ab_api_key table to the existence check in _create_db(), which means create_all() now runs on fresh databases where it previously didn't — hitting the type mismatch.

The fix aligns the ORM model with the actual database schema. No migration is needed since every existing database already has the correct VARCHAR(11) type from the original migration.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A — backend model fix only.

TESTING INSTRUCTIONS

  1. Verify String is already imported in superset/models/sql_lab.py (line 43)
  2. Confirm the original migration uses sa.String(11) for this column: superset/migrations/versions/2019-11-13_11-05_db4b49eb0782_add_tables_for_sql_lab_state.py line 50
  3. Confirm Query.client_id is String(11) at line 116 of superset/models/sql_lab.py
  4. Any test that creates tables via Model.metadata.create_all() on a fresh PostgreSQL database will now succeed

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
  • Introduces new feature or API
  • Removes existing feature or API

CodeAnt-AI Description

Fix database creation for saved SQL tabs on fresh installs

What Changed

  • Saved SQL tab records now store the latest query ID using the same text format as the query record it points to.
  • This prevents database setup from failing when creating tables on a new PostgreSQL database.
  • Existing databases are unaffected because they already use the correct column format.

Impact

✅ Fewer fresh-install database failures
✅ Successful setup for SQL Lab on new databases
✅ Clearer upgrade path for FAB-based installs

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

…r to String(11)

The ORM model defined latest_query_id as Integer, but it references
Query.client_id which is String(11). The original 2019 migration
correctly used String(11). This mismatch was exposed by the FAB 5.1.0
to 5.2.0 upgrade, which now calls Model.metadata.create_all() in
create_db() when the new ab_api_key table doesn't exist yet.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sadpandajoe sadpandajoe marked this pull request as draft March 25, 2026 04:58
@dosubot dosubot Bot added the change:backend Requires changing the backend label Mar 25, 2026
@codeant-ai-for-open-source codeant-ai-for-open-source Bot added the size:XS This PR changes 0-9 lines, ignoring generated files label Mar 25, 2026
@codeant-ai-for-open-source
Copy link
Copy Markdown
Contributor

Sequence Diagram

This PR fixes the ORM definition of TabState latest_query_id to use String11 so it matches Query client_id. With matching types, metadata create all can create the foreign key on fresh databases without datatype mismatch failures.

sequenceDiagram
    participant Setup as SecurityManager
    participant ORM as SQLAlchemy Models
    participant DB as PostgreSQL

    Setup->>ORM: Run metadata create all
    ORM->>ORM: Define TabState latest_query_id as String11
    ORM->>DB: Create query and tab_state tables
    DB->>DB: Validate FK type compatibility
    DB-->>ORM: Create FK latest_query_id to query client_id
    ORM-->>Setup: Database setup succeeds
Loading

Generated by CodeAnt AI

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Aligns the SQL Lab ORM model with the existing database schema by correcting the tab_state.latest_query_id column type to match the FK target (query.client_id). This prevents metadata.create_all() flows (e.g., FAB create_db() on fresh installs) from failing due to an FK type mismatch, while leaving migrated/existing databases unchanged.

Changes:

  • Change TabState.latest_query_id from Integer to String(11) to match Query.client_id.
  • Ensures FK creation succeeds when tables are created from ORM metadata (not only via Alembic migrations).

@sadpandajoe sadpandajoe marked this pull request as ready for review March 25, 2026 05:29
@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review Bot commented Mar 25, 2026

Code Review Agent Run #6ea7e7

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: a98de58..a98de58
    • superset/models/sql_lab.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ 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

@aminghadersohi aminghadersohi merged commit 4b26f8c into master Mar 25, 2026
117 of 119 checks passed
@aminghadersohi aminghadersohi deleted the fix-fab-upgrade branch March 25, 2026 08:24
michael-s-molina pushed a commit that referenced this pull request Mar 26, 2026
… target (#38837)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
(cherry picked from commit 4b26f8c)
qfcwell pushed a commit to qfcwell/superset that referenced this pull request May 12, 2026
… target (apache#38837)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:backend Requires changing the backend size/XS size:XS This PR changes 0-9 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants