GitHub user dosubot[bot] added a comment to the discussion: superset5 to 
superset6 migration

<!-- Answer -->
This error occurs during the [themes table migration 
(`3fd555e76e3d`)](https://github.com/apache/superset/discussions/37232#discussioncomment-15540179)
 when adding a foreign key constraint. The most common cause is **orphaned 
records** in the `themes` table that reference non-existent users in `ab_user`.

**To fix this, first check for orphaned records:**
```sql
SELECT created_by_fk FROM themes 
WHERE created_by_fk IS NOT NULL 
AND created_by_fk NOT IN (SELECT id FROM ab_user);
```

**If orphaned records exist, clean them up:**
```sql
-- Option 1: Set to NULL
UPDATE themes SET created_by_fk = NULL 
WHERE created_by_fk NOT IN (SELECT id FROM ab_user);

-- Option 2: Delete the records
DELETE FROM themes 
WHERE created_by_fk NOT IN (SELECT id FROM ab_user);
```

Then re-run `superset db upgrade`.

**Other potential causes:**
- [Multi-process migrations (e.g., multiple Kubernetes 
pods)](https://github.com/apache/superset/issues/34403#issuecomment-3136692427) 
- scale down all replicas to 0 before running migrations
- Version mismatch between your code and Docker image - ensure both are at the 
same version

Let me know if cleaning up orphaned records doesn't resolve the issue.


<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot).*

---
<sup>How did I do? 
[Good](https://app.dosu.dev/response-feedback/bf795ee9-81a5-4151-afd1-43e75bb246e2?feedback_type=great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/bf795ee9-81a5-4151-afd1-43e75bb246e2?feedback_type=irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/bf795ee9-81a5-4151-afd1-43e75bb246e2?feedback_type=incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/bf795ee9-81a5-4151-afd1-43e75bb246e2?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/bf795ee9-81a5-4151-afd1-43e75bb246e2?feedback_type=hallucination)
 | [Report 
🐛](https://app.dosu.dev/response-feedback/bf795ee9-81a5-4151-afd1-43e75bb246e2?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/bf795ee9-81a5-4151-afd1-43e75bb246e2?feedback_type=other)</sup>

[![Chat with 
Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github)&nbsp;[![Open
 in 
Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20error%20occurs%20during%20the%20%5Bthemes%20table%20migration%20%28%603fd555e76e3d%60%29%5D%28https%3A//github.com/apache/superset/discussions/37232%23discussioncomment-15540179%29%20when%20adding%20a%20foreign%20key%20constraint.%20The%20most%20common%20cause%20is%20%2A%2Aorphaned%20records%2A%2A%20in%20the%20%60themes%60%20table%20that%20reference%20non-existent%20users%20in%20%60ab_user%60.%0A%0A%2A%2ATo%20fix%20this%2C%20first%20check%20for%20orphaned%20records%3A%2A%2A%0A%60%60%60sql%0ASELECT%20created_by_fk%20FROM%20themes%20%0AWHERE%20created_by_fk%20IS%20NOT%20NULL%20%0AAND%20created_by_fk%20NOT%20IN%20%28SELECT%20id%20FROM%20ab_user%29%3B%0A%60%60%60%0A%0A%2A%2AIf%20orphaned%20records%20exist%2C%20clean%20them%20up%3A%2A%2A%0A%60%60%60
 
sql%0A--%20Option%201%3A%20Set%20to%20NULL%0AUPDATE%20themes%20SET%20created_by_fk%20%3D%20NULL%20%0AWHERE%20created_by_fk%20NOT%20IN%20%28SELECT%20id%20FROM%20ab_user%29%3B%0A%0A--%20Option%202%3A%20Delete%20the%20records%0ADELETE%20FROM%20themes%20%0AWHERE%20created_by_fk%20NOT%20IN%20%28SELECT%20id%20FROM%20ab_user%29%3B%0A%60%60%60%0A%0AThen%20re-run%20%60superset%20db%20upgrade%60.%0A%0A%2A%2AOther%20potential%20causes%3A%2A%2A%0A-%20%5BMulti-process%20migrations%20%28e.g.%2C%20multiple%20Kubernetes%20pods%29%5D%28https%3A//github.com/apache/superset/issues/34403%23issuecomment-3136692427%29%20-%20scale%20down%20all%20replicas%20to%200%20before%20running%20migrations%0A-%20Version%20mismatch%20between%20your%20code%20and%20Docker%20image%20-%20ensure%20both%20are%20at%20the%20same%20version%0A%0ALet%20me%20know%20if%20cleaning%20up%20orphaned%20records%20doesn%27t%20resolve%20the%20issue.)&nbsp;[![Join
 Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=whi
 te&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on 
X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/discussions/38286)

GitHub link: 
https://github.com/apache/superset/discussions/38286#discussioncomment-15943178

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: 
[email protected]


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

Reply via email to