cloudflare / cloudflare/developer-platform

🐛 BUG: `d1 export` produces unimportable sql files

オープン
#41 コメント 4 件 リアクション 5 件 担当者 0 名 GitHub で見る
product:d1
主要言語
言語のデータがありません
スター
1
フォーク
0
PR マージ指標
30日以内にマージされた PR はありません

説明

### Which Cloudflare product(s) does this pertain to?

D1

### What version(s) of the tool(s) are you using?

3.51.2 [Wrangler]

### What version of Node are you using?

21.6.10

### What operating system and version are you using?

macOS Sonoma 14.4.1

### Describe the Bug

### Observed behavior
When exporting a D1 database with some relations using `npx wrangler d1 export`, it can generate an SQL file that cannot be imported again (`✘ [ERROR] no such table: main.B`). This is due to INSERTs happening before all tables are created.
I've prepared a minimal reproduction repository: https://github.com/hrueger/d1-export-order

### Expected behavior
All `CREATE TABLE` queries happen first, all `INSERT`s afterwards.

### Steps to reproduce
See https://github.com/hrueger/d1-export-order

in short, you need two tables, where the first table has a foreign key to the second table. Insert one row each, export that and try to import it again using `npx wrangler execute ... --file ...`

In the minimal reproduction there's also a fixed sql file, where the `INSERT` is just moved to the bottom.

### Please provide a link to a minimal reproduction

https://github.com/hrueger/d1-export-order

### Please provide any relevant error logs

```
✘ [ERROR] no such table: main.B
```

As per our discord conversation on friday, I'm tagging @geelen. Thanks for looking into this!

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

最小再現リポジトリから始めて `npx wrangler d1 export` を実行し、生成されたファイルを `npx wrangler execute ... --file ...` でインポートしてみてください。生成された SQL と修正済みの SQL ファイルを比較し、外部キー関係を含むエクスポートを正常にインポートでき、すべてのテーブルが行の挿入前に作成されていることを確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
node.js, sql
領域
cli, database
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
静か
明瞭さ
明確に書かれている
初心者へのやさしさ
68/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。