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

Offen Anfängerfreundlich
#162,646 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@vikash1311994 arbeitet bereits daran.

Seit 26.11.2026.

  • #162662 von @vikash1311994 — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
62/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python, sqlite
Bereich
databases

Rechercherichtung

Der Fehler liegt in Lib/sqlite3/dump.py: Die Dump-Abfrage SELECT * FROM "sqlite_sequence" wird auf ein verbliebenes temp.sqlite_sequence statt auf main aufgelöst. Beginne damit, den Fehler mit dem Snippet aus dem Issue zu reproduzieren. Qualifiziere die Abfrage dann als main.sqlite_sequence und prüfe das ausgegebene INSERT für ('invoices', 2). Fertig ist es, wenn die wiederhergestellte Datenbank die ID 3 vergibt und ein Regressionstest einen gelöschten temporären AUTOINCREMENT-Tabellen-Fall vor iterdump() abdeckt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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

Vorherrschende Sprache
Python
Sterne
78.4k
Forks
36.6k
Ø Merge
1 T. 5 Std.
Gemergte PRs (30 T.)
615

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/cpython

Alle Issues in python/cpython

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.