Skip to content

Align SQLite row values with their column names - #86

Merged
xrotwang merged 1 commit into
cldf:masterfrom
antonsoo:fix/database-row-column-order
Oct 24, 2026
Merged

xrotwang merged 1 commit into
cldf:masterfrom
antonsoo:fix/database-row-column-order

Conversation

@antonsoo

Copy link
Copy Markdown
Contributor

Database.write takes column names from the first row, but takes each later row's values in that dictionary's insertion order. This silently swaps values when otherwise equivalent dictionaries have different key orders:

from csvw import TableGroup
from csvw.db import Database

tg = TableGroup.fromvalue({'tables': [{
    'url': 'forms',
    'tableSchema': {'columns': [{'name': 'ID'}, {'name': 'Form'}]},
}]})
db = Database(tg)
db.write(forms=[{'ID': '1', 'Form': 'house'}, {'Form': 'water', 'ID': '2'}])
print(dict(db.read()['forms'][1]))

This prints {'ID': 'water', 'Form': '2'}; it should print {'ID': '2', 'Form': 'water'}.

Collect converted values by column name, then emit them in the first row's column order. Reject rows with different column sets so an extra or missing field cannot be silently discarded or assigned to another column.

Four regressions fail before the change and pass after. The full suite passes on Python 3.12 (829 passed, 16 expected failures) and 3.9 (832 passed, 13 expected failures); Flake8 passes for the changed files. A recorded lexical-data round trip also preserves all 1,514 rows through memory/file databases, translated column names and CSV export. Previously, alternating dictionary orders corrupted 757 rows and rotating orders corrupted 1,135; stable-order controls remain identical.

@xrotwang
xrotwang merged commit efdd568 into cldf:master Oct 24, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants