sqlite3.Connection.iterdump() can lose AUTOINCREMENT counters after a temporary table is dropped

オープン 初心者向け
#162,646 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@vikash1311994 がすでに取り組んでいます。

2026年11月26日 から。

  • #162662 @vikash1311994 による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
62/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, sqlite
領域
databases

調査の方向性

バグは Lib/sqlite3/dump.py にあります。ダンプ用のクエリ SELECT * FROM "sqlite_sequence" が、main ではなく残存している temp.sqlite_sequence に解決されています。まず issue のスナップショットで再現させ、そのうえでクエリを main.sqlite_sequence と修飾し、出力された ('invoices', 2) の INSERT を確認してください。復元したデータベースが ID 3 を割り当て、iterdump() の前に削除された一時 AUTOINCREMENT テーブルをリグレッションテストがカバーしていれば完了です。

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

説明

stdlib topic-sqlite3 type-bug
Bug report

Restoring a database from Connection.iterdump() can cause an AUTOINCREMENT table to reuse a previously issued ID. This happens even when the temporary table that triggered the problem had a different name and was dropped before the dump.

import sqlite3

source = sqlite3.connect(":memory:")
source.executescript("""
    CREATE TABLE invoices(id INTEGER PRIMARY KEY AUTOINCREMENT);
    INSERT INTO invoices DEFAULT VALUES;
    INSERT INTO invoices DEFAULT VALUES;
    DELETE FROM invoices WHERE id = 2;

    CREATE TEMP TABLE staging(id INTEGER PRIMARY KEY AUTOINCREMENT);
    DROP TABLE temp.staging;
""")

restored = sqlite3.connect(":memory:")
restored.executescript("\n".join(source.iterdump()))

for db in (source, restored):
    print(db.execute("INSERT INTO invoices DEFAULT VALUES").lastrowid)

Expected: both inserts allocate ID 3.
Actual: the source allocates ID 3 and the restored database reuses ID 2.

Before the insert, both databases contain only invoice 1 and both pass PRAGMA integrity_check. A copy made with Connection.backup() correctly allocates ID 3. Removing the two staging-table statements also makes the SQL restore allocate ID 3.

Creating the temporary table leaves an empty temp.sqlite_sequence behind after the table is dropped. In Lib/sqlite3/dump.py, SELECT * FROM "sqlite_sequence" resolves to that temporary table. The dump consequently emits DELETE FROM "sqlite_sequence" without the INSERT that would restore the ('invoices', 2) counter. Qualifying the source query as main.sqlite_sequence fixes this reproducer.

CPython versions tested on:

3.12, 3.13, 3.14, 3.15, 3.16, CPython main branch

Operating systems tested on:

Linux

主要言語
Python
スター
78.4k
フォーク
36.6k
平均マージ
1日 5時間
マージ済み PR(30日)
615

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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