Skip to content

One Column class: db.table_column_details() returns datasette.Column - #3101

Merged
simonw merged 3 commits into
mainfrom
unify-column-classes
Nov 22, 2026
Merged

simonw merged 3 commits into
mainfrom
unify-column-classes

Conversation

@simonw

@simonw simonw commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Closes:

There were two classes called Column: datasette.Column, describing a column of a row, and the datasette.utils.Column named tuple returned by db.table_column_details(). This removes the named tuple and uses datasette.Column for both.

Claude Fable 5.2 High

What changed

  • datasette.Column gains the attributes the named tuple carried: notnull, default_value, pk_position and hidden, plus a new generated. They are None for the columns of an arbitrary SQL query, the same as type.
  • hidden is a boolean, True only for a hidden column of a virtual table. generated is "virtual" or "stored" for a generated column and None otherwise. The named tuple had both in one hidden integer, using the numbers from SQLite's PRAGMA table_xinfo (1 for hidden, 2 or 3 for generated).
  • db.table_column_details(table) and datasette.utils.table_column_details(conn, table) return a Columns object of datasette.Column. columns["name"] looks a column up by name.
  • datasette.utils.Column is deleted.
  • db.describe_columns() is deleted. It was added in datasette.Row replaces sqlite3.Row and CustomRow #3099 and never released. The table and row views now attach the columns from table_column_details() to their rows.
  • Column.table, Column.foreign_key and the ForeignKey class are deleted. They were also added in datasette.Row replaces sqlite3.Row and CustomRow #3099 and nothing in core read them. Foreign keys are still available from db.foreign_keys_for_table().

Backwards incompatible

For code that uses db.table_column_details():

  • is_pk is now pk_position. It always held the position in the primary key, not a boolean.
  • type is None for a column declared without a type. It was an empty string.
  • hidden is a boolean and no longer true for generated columns. Code that used if column.hidden to skip columns that cannot be written to needs if column.hidden or column.generated.
  • cid is gone. It was the position of the column in the list.
  • Columns are no longer tuples, so they cannot be unpacked or indexed by position.
  • The return value is a Columns object, not a list. It can be looped over and indexed the same way.

name, type, notnull and default_value are otherwise read exactly as before. docs/upgrade_guide.md has a new section with before and after code.

How this differs from the spec posted on the issue

That spec was written before #3099 was finished and is out of date in places. This PR follows a simpler version:

  • type stays a plain string. The spec assumed a DatabaseType class, which was removed before datasette.Row replaces sqlite3.Row and CustomRow #3099 merged.
  • generated is a string, "virtual" or "stored". The spec proposed a boolean. The string is needed because the column_details JSON extra documents different values for the two kinds, and that output is unchanged here.
  • Only is_pk is renamed. The spec also renamed default_value to default.
  • table_column_details() does not look up foreign keys. The spec had it run an extra PRAGMA foreign_key_list on every call, and a table page calls it 5 to 10 times. Dropping Column.foreign_key avoids that.

Unchanged output

  • The catalog_columns table in the internal database keeps its schema and contents, including cid and is_pk.
  • The column_details JSON extra still returns type as "" for an untyped column, and both is_pk and pk_position.
  • Both still report hidden as SQLite's integer, 0 to 3. It is rebuilt from the two new attributes by datasette.utils.table_xinfo_hidden(column).

To check this I captured, before and after the change, everything derived from column details for all 37 tables and views in the fixtures database plus three extra tables (generated columns, a compound primary key in non-table order, and a table with no primary key): the column_details, columns, display_columns, primary_keys and column_types JSON extras, the table page's template context (including the insert, edit and alter form data), every row of catalog_columns, and the results of db.primary_keys() and db.table_columns(). All 150 outputs are identical.

Performance

Per call, on a 10-column table, best of three runs against main:

main this branch
table_column_details() 10.8 µs 18.2 µs
detect_primary_keys() 11.2 µs 9.8 µs
table_columns() 11.2 µs 9.0 µs

table_column_details() is slower because a frozen dataclass costs more to construct than a named tuple, and it now has one more attribute to fill in. detect_primary_keys() and table_columns() now read the PRAGMA rows directly without building Column objects, so they are faster than before. I did not measure whole pages for this PR.

Testing

  • Full suite locally: 3,526 passed. black, ruff, codespell, cog, blacken-docs and a strict Sphinx build pass. datasette/rows.py passes mypy --strict.
  • New tests cover the attributes returned by db.table_column_details(), a compound primary key whose order differs from the table's column order, virtual and stored generated columns, the hidden columns of an FTS5 table, and how the columns of a row are derived for the implicit rowid and for names that are not columns of the table.
  • Not run locally on SQLite 3.25, where table_xinfo is unavailable and a fallback path is used. The CI job for 3.25 covers it.

🤖 Generated with Claude Code

…loses #3100

datasette.utils.Column, a named tuple, is removed. datasette.Column gains
the notnull, default_value, pk_position and hidden attributes it carried,
and db.table_column_details() returns a Columns object of them.

- is_pk is now pk_position, cid is dropped, and type is None instead of ""
  for a column declared without a type
- Column.table, Column.foreign_key and the ForeignKey class are removed.
  Nothing in core read them, and filling them in for every call would have
  cost an extra query each time
- db.describe_columns(), which was never released, is removed. The table
  and row views attach the columns from table_column_details() instead
- The catalog_columns table and all JSON output are unchanged

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 38 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (1ad8e36) to head (9042cba).

Files with missing lines Patch % Lines
datasette/utils/__init__.py 0.00% 17 Missing ⚠️
datasette/database.py 0.00% 7 Missing ⚠️
datasette/rows.py 0.00% 5 Missing ⚠️
datasette/views/table.py 0.00% 4 Missing ⚠️
datasette/views/table_create_alter.py 0.00% 2 Missing ⚠️
datasette/utils/internal_db.py 0.00% 1 Missing ⚠️
datasette/views/execute_write.py 0.00% 1 Missing ⚠️
datasette/views/row.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main   #3101   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files         81      81           
  Lines      14516   14512    -4     
=====================================
+ Misses     14516   14512    -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

SQLite 3.25 reports a declared type exactly as it was written, so
a column declared as text came back as 'text' where the test
expected 'TEXT'.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@simonw

simonw commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner Author

I was unconvinced by this:

The .foreign_key attribute a column has two problems though: SQLite supports compound foreign keys (not represented by a single attribute), plus it's possible for one column to have more than one foreign key constraint. So removing .foreign_key is a good call.

And column.table isn't needed by anything, since we always know what column we are dealing with by the time we introspect its columns.

Comment thread docs/internals.rst Resolved
@simonw simonw added this to the Datasette 1.0 stable milestone Nov 22, 2026
Column.hidden was the integer from PRAGMA table_xinfo: 1 for a hidden
column of a virtual table, 2 or 3 for a generated column. It is now
True only for a hidden virtual table column, and Column.generated is
"virtual", "stored" or None.

The column_details JSON extra and the catalog_columns table still
report SQLite's integer, rebuilt by table_xinfo_hidden().

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@simonw
simonw merged commit 67ece91 into main Nov 22, 2026
16 checks passed
@simonw
simonw deleted the unify-column-classes branch November 22, 2026 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant