Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3130,10 +3130,24 @@ def fk_with_renamed_columns(fk: ForeignKey) -> ForeignKey:
for from_, to_ in copy_from_to.items():
old_cols.append(from_)
new_cols.append(to_)
# Ensure rowid is copied too
if "rowid" not in new_cols:
new_cols.insert(0, "rowid")
old_cols.insert(0, "rowid")

# A declared column can shadow a rowid alias on either side of a rename.
def rowid_alias(columns: Iterable[str]) -> str | None:
declared = {fold_identifier_case(column) for column in columns}
return next(
(
alias
for alias in ("rowid", "_rowid_", "oid")
if alias not in declared

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve declared INTEGER PRIMARY KEY row-ID aliases

When a table declares rowid INTEGER PRIMARY KEY alongside _rowid_ TEXT and oid TEXT, rowid still aliases the actual integer row ID, but this helper treats all three names as shadowed and returns None. Starting with a single row whose ID is 42, transform(drop={'rowid'}, pk=None) consequently skips the hidden-ID copy and silently renumbers that row to 1; the parent commit preserved 42. Allow an integer-primary-key alias to supply source row IDs even when every magic alias is declared.

Useful? React with 👍 / 👎.

),
None,
)

source_rowid = rowid_alias(existing_columns)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Skip hidden row-ID copying for WITHOUT ROWID sources

For a populated CREATE TABLE nodes (rowid TEXT PRIMARY KEY, name TEXT) WITHOUT ROWID, transform(rename={'name': 'label'}) succeeded before this change but now raises IntegrityError: datatype mismatch. The helper selects _rowid_ merely because it is undeclared, although this source has no hidden row ID. With SQLite's default double-quoted-string behavior, the generated SELECT "_rowid_" produces a string literal that cannot be inserted into the destination's hidden integer ID. Check whether the source actually has a row ID before adding this copy expression.

Useful? React with 👍 / 👎.

target_rowid = rowid_alias(new_cols)
if source_rowid and target_rowid:
new_cols.insert(0, target_rowid)
old_cols.insert(0, source_rowid)

def _copy_expr(col):
if col in text_to_numeric_cols:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,25 @@ def test_transform_preserves_rowids(fresh_db, table_type):
assert previous_rows == next_rows


@pytest.mark.parametrize(
"rename,user_id,hidden_id",
[
({"name": "label"}, "RowID", "oid"),
({"name": "label", "RowID": "oid"}, "oid", "rowid"),
],
)
def test_transform_preserves_shadowed_rowid(fresh_db, rename, user_id, hidden_id):
fresh_db.execute("create table nodes (RowID text, _rowid_ text, name text)")
fresh_db.execute(
"insert into nodes (oid, RowID, _rowid_, name) values (42, 'a', 'b', 'c')"
)
fresh_db.table("nodes").transform(rename=rename)
row = fresh_db.execute(
f"select {hidden_id}, {user_id}, _rowid_, label from nodes"
).fetchone()
assert tuple(row) == (42, "a", "b", "c")


@pytest.mark.parametrize(
"initial_strict,transform_strict,expected_strict",
(
Expand Down