mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-24 01:44:31 +02:00
Fix transform() dropping AUTOINCREMENT from INTEGER PRIMARY KEY columns
Calling .transform() on a table with INTEGER PRIMARY KEY AUTOINCREMENT recreated the table without the AUTOINCREMENT keyword, silently changing the table's behaviour so that deleted row IDs could be reused. The fix adds an autoincrement parameter to create_table_sql() and detects AUTOINCREMENT in the existing schema before calling it from transform_sql().
This commit is contained in:
parent
d714200659
commit
05b5f0fe26
2 changed files with 25 additions and 0 deletions
|
|
@ -1400,6 +1400,7 @@ class Database:
|
|||
extracts: Optional[Union[Dict[str, str], List[str]]] = None,
|
||||
if_not_exists: bool = False,
|
||||
strict: bool = False,
|
||||
autoincrement: bool = False,
|
||||
) -> str:
|
||||
"""
|
||||
Returns the SQL ``CREATE TABLE`` statement for creating the specified table.
|
||||
|
|
@ -1496,6 +1497,8 @@ class Database:
|
|||
column_extras = []
|
||||
if column_name == single_pk:
|
||||
column_extras.append("PRIMARY KEY")
|
||||
if autoincrement:
|
||||
column_extras.append("AUTOINCREMENT")
|
||||
if column_name in not_null:
|
||||
column_extras.append("NOT NULL")
|
||||
if column_name in defaults and defaults[column_name] is not None:
|
||||
|
|
@ -2807,6 +2810,8 @@ class Table(Queryable):
|
|||
if column_order is not None:
|
||||
column_order = [rename.get(col) or col for col in column_order]
|
||||
|
||||
has_autoincrement = "autoincrement" in self.schema.lower()
|
||||
|
||||
sqls = []
|
||||
sqls.append(
|
||||
self.db.create_table_sql(
|
||||
|
|
@ -2818,6 +2823,7 @@ class Table(Queryable):
|
|||
foreign_keys=create_table_foreign_keys,
|
||||
column_order=column_order,
|
||||
strict=self.strict if strict is None else strict,
|
||||
autoincrement=has_autoincrement,
|
||||
).strip()
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue