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

Aperta Adatta ai principianti
#162,646 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@vikash1311994 ci sta già lavorando.

Dal 26/11/2026.

  • #162662 di @vikash1311994 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, sqlite
Ambito
databases

Direzione di ricerca

Il bug si trova in Lib/sqlite3/dump.py, dove la query di dump SELECT * FROM "sqlite_sequence" si risolve in un temp.sqlite_sequence residuo invece che in main. Inizia riproducendo il problema con lo snippet della issue, poi qualifica la query come main.sqlite_sequence e verifica l'INSERT emesso per ('invoices', 2). Il lavoro è completo quando il database ripristinato assegna l'ID 3 e un test di regressione copre una tabella temporanea AUTOINCREMENT eliminata prima di iterdump().

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Python
Stelle
78.4k
Fork
36.6k
Merge medio
1g 5h
PR unite (30g)
615

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python/cpython

Tutte le issue di python/cpython

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.