fix(models): correct TabState.latest_query_id column type to match FK target#38837
Conversation
…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>
Sequence DiagramThis 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
Generated by CodeAnt AI |
There was a problem hiding this comment.
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_idfromIntegertoString(11)to matchQuery.client_id. - Ensures FK creation succeeds when tables are created from ORM metadata (not only via Alembic migrations).
Code Review Agent Run #6ea7e7Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
… target (apache#38837) Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
User description
SUMMARY
TabState.latest_query_idis defined asIntegerin the ORM model, but it referencesQuery.client_idwhich isString(11). The original migration from 2019 correctly created the column asVARCHAR(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'sSecurityManager.create_db()— will attempt to create the table using the ORM definition and fail with:This was exposed by the FAB 5.1.0 → 5.2.0 upgrade in #37973. FAB 5.2.0 added the new
ab_api_keytable to the existence check in_create_db(), which meanscreate_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
Stringis already imported insuperset/models/sql_lab.py(line 43)sa.String(11)for this column:superset/migrations/versions/2019-11-13_11-05_db4b49eb0782_add_tables_for_sql_lab_state.pyline 50Query.client_idisString(11)at line 116 ofsuperset/models/sql_lab.pyModel.metadata.create_all()on a fresh PostgreSQL database will now succeedADDITIONAL INFORMATION
CodeAnt-AI Description
Fix database creation for saved SQL tabs on fresh installs
What Changed
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:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
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:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
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.