Repository navigation
Let a column name its implicit sequence with duckdb_sequence_name - #229
Merged
leonardovida merged 2 commits intoNov 17, 2026
Merged
Conversation
…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`.
Contributor
Author
|
Thanks! 🙏 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
duckdb_sequence_namecolumn 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 theSEQUENCE NAMEof its identity column, so clients that callnextval('seq_users')work on both.Columnoption becauseIdentityaccepts no dialect options before SQLAlchemy 2.1.IF NOT EXISTS, so creating the table fails if a sequence of that name exists, rather than adopting one another table may use.RETURNINGdraw from it, and Alembic batch recreates withcopy_fromkeep it.CompileErrorbefore anything is created.op.drop_tablecannot tell which sequence to drop, so the docs show the downgrade dropping it withop.execute. DuckDB'sALTER SEQUENCE ... OWNED BYcannot 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.mdhas no entry.A separate commit fixes
test_sweep_regressionsimportingduckdb_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 onmain.pre-commitpasses.Resolves #227