Skip to content

Let a column name its implicit sequence with duckdb_sequence_name - #229

Merged
leonardovida merged 2 commits into
leonardovida:mainfrom
dhirschfeld:feat/identity-sequence-name
Nov 17, 2026
Merged

leonardovida merged 2 commits into
leonardovida:mainfrom
dhirschfeld:feat/identity-sequence-name

Conversation

@dhirschfeld

@dhirschfeld dhirschfeld commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a duckdb_sequence_name column option that names the sequence the dialect creates for an autoincrement integer primary key, instead of <table>_<column>_seq. A model shared with PostgreSQL can then use the same sequence name on DuckDB as the SEQUENCE NAME of its identity column, so clients that call nextval('seq_users') work on both.

Column("id", BigInteger, Identity(), primary_key=True, duckdb_sequence_name="seq_users")
  • It is a Column option because Identity accepts no dialect options before SQLAlchemy 2.1.
  • The sequence is created in the table's schema and dropped with the table. It is created without IF NOT EXISTS, so creating the table fails if a sequence of that name exists, rather than adopting one another table may use.
  • Inserts without RETURNING draw from it, and Alembic batch recreates with copy_from keep it.
  • On a column the dialect does not back with a sequence, the option raises CompileError before anything is created.
  • Alembic's op.drop_table cannot tell which sequence to drop, so the docs show the downgrade dropping it with op.execute. DuckDB's ALTER SEQUENCE ... OWNED BY cannot do this: the ownership is lost when a database file is reopened, and it stops a batch recreate from dropping the original table.

Existing behaviour is unchanged, so docs/breaking-changes.md has no entry.

A separate commit fixes test_sweep_regressions importing duckdb_sqlalchemy.__init__, which loaded a second copy of the module, so every implicit sequence was created and dropped twice.

Tests

End-to-end tests in duckdb_sqlalchemy/tests/e2e/test_sequence_names.py. The full suite passes on SQLAlchemy 2.0.52 and 2.1.1. On 2.0.0 the only failures are the 35 pandas tests that also fail on main. pre-commit passes.


Resolves #227

…sions`

Importing it from `duckdb_sqlalchemy.__init__` loaded a second copy of the
module. Each copy registered its own `before_create` and `after_drop`
listeners on `Table`, so every implicit sequence was created and dropped
twice once that test module had been imported. `IF NOT EXISTS` and
`IF EXISTS` hid the duplicate statements.
An autoincrement integer primary key, including one declared with
`Identity()`, is backed by a sequence the dialect always named
`<table>_<column>_seq`. A model shared with PostgreSQL could therefore not
keep the `SEQUENCE NAME` of its identity column, which clients that call
`nextval()` rely on.

`Column(..., duckdb_sequence_name="seq_users")` now chooses the name. It is
a `Column` option because `Identity` accepts no dialect options before
SQLAlchemy 2.1.

- The sequence is created in the table's schema without `IF NOT EXISTS`, as
  PostgreSQL creates an identity column's sequence, so a table cannot adopt
  a sequence another table uses and then drop it. It is dropped with the
  table.
- Inserts without `RETURNING` draw from it, following `schema_translate_map`.
- Alembic batch recreates with `copy_from` keep the existing sequence.
- The option raises `CompileError` on a column the dialect does not back
  with a sequence, before any sequence is created.

DuckDB cannot make a table own its sequence: `ALTER SEQUENCE ... OWNED BY`
is forgotten when a database file is reopened, and an owned sequence stops
an Alembic batch recreate from dropping the original table. Alembic's
`op.drop_table` cannot tell which sequence to drop, so the docs tell such
downgrades to drop it with `op.execute`.
@leonardovida
leonardovida merged commit ffa2763 into leonardovida:main Nov 17, 2026
26 checks passed
@dhirschfeld

Copy link
Copy Markdown
Contributor Author

Thanks! 🙏

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.

[Feature]: Let an Identity primary key name its DuckDB sequence

2 participants