justinpark commented on PR #39859:
URL: https://github.com/apache/superset/pull/39859#issuecomment-4425770485

   I ran the attached the upgrade script and encountered the following error.
   ```
   MySQLdb.OperationalError: (1832, "Cannot change column 'dashboard_id': used 
in a foreign key constraint 'fk_dashboard_roles_dashboard_id_dashboards'")
   ```
   I found the following fix with the help of Claude.
   
   ```
   Apply the same MySQL FK-drop logic to the else branch:
   
   else:
       if conn.dialect.name == "mysql":
           for fk in insp.get_foreign_keys(t.name):
               if fk_name := fk.get("name"):
                   op.drop_constraint(fk_name, t.name, type_="foreignkey")
       with op.batch_alter_table(t.name) as batch_op:
           batch_op.drop_column("id")
           batch_op.create_primary_key(f"pk_{t.name}", [t.fk1, t.fk2])
           batch_op.alter_column(t.fk1, existing_type=sa.Integer, 
nullable=False)
           batch_op.alter_column(t.fk2, existing_type=sa.Integer, 
nullable=False)
   
   However, the cleaner fix is to unify the two branches since the MySQL 
FK-drop logic is now identical — the only real difference between them is 
whether a pre-existing UNIQUE constraint needs to be dropped, not how MySQL FKs 
are handled:
   
   # Drop FKs first on MySQL for ALL tables (not just 
TABLES_WITH_PRE_EXISTING_UNIQUE)
   if conn.dialect.name == "mysql":
       for fk in insp.get_foreign_keys(t.name):
           if fk_name := fk.get("name"):
               op.drop_constraint(fk_name, t.name, type_="foreignkey")
   
   if t.name in TABLES_WITH_PRE_EXISTING_UNIQUE:
       with op.batch_alter_table(
           t.name,
           recreate="always",
           copy_from=_build_pre_upgrade_table(insp, t),
       ) as batch_op:
           ...
   else:
       with op.batch_alter_table(t.name) as batch_op:
           ...
   
   This separates the two concerns cleanly — MySQL FK handling applies to all 
tables, and the recreate="always" path is only for tables with a pre-existing 
UNIQUE.
   
   The downgrade() function must mirror this in reverse order.
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to