Workers SDK Issue Reports

← Back to Dashboard

#5683 d1 export produces unimportable sql files

Recommendation:KEEP OPEN
Difficulty:easy
Reasoning:

Valid bug; INSERTs before CREATE TABLEs; workaround available

Suggested Action:

Fix export statement ordering

Analysis Report

Issue Review: cloudflare/workers-sdk#5683

Summary

d1 export produces SQL files that cannot be imported due to INSERTs executing before CREATE TABLEs for tables with foreign key relationships.

Findings

  • Created: 2024-04-22
  • Updated: 2025-10-30
  • Version: wrangler 3.51.2 → wrangler 4.61.0
  • Component: wrangler (D1)
  • Labels: bug, d1
  • Comments: 2

Key Evidence

  • Reproduction repo: https://github.com/hrueger/d1-export-order
  • Issue: Tables with foreign keys get INSERT statements interleaved with CREATE TABLE
  • Error on import: [ERROR] no such table: main.B
  • Workaround provided by reporter: JavaScript to reorder SQL statements
  • Expected: All CREATE TABLE statements first, then all INSERTs
  • Low comment count but 8 reactions indicates many affected users

Recommendation

Status: KEEP OPEN

Reasoning: This is a valid bug in the D1 export functionality. The exported SQL is invalid because statement ordering doesn't respect foreign key dependencies. Users have provided a workaround but the core issue should be fixed.

Action: Fix the D1 export logic to output all CREATE TABLE statements before any INSERT statements, or properly order based on foreign key dependencies.

Suggested Comment

N/A - Issue is valid and has a workaround documented.

Notes & Feedback (0)

No notes yet.

Add Note