Fixed #35334 -- Removed supports_sequence_reset feature flag #18024
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.
https://code.djangoproject.com/ticket/35334
Draft PR
There are a few issues with sequences resetting:
Notes about how sequences currently work in Django
sequence_reset_by_name_sql()
If supported explicitly sets the sequence to 1
Called by:
sql_flush()
sql_flush()
see note belowTransactionTestCase._fixture_setup()
ifreset_sequences = True
Note: Despite comments in oracle.operations.sql_flush():
there is no ALTER command issued, neither do the sequences get reset.
sequence_reset_sql()
If supported by the backend re-aligns the sequence with the table's contents
Called by:
contrib.sites
'create_default_site()
on post_migrateloadddata
mgt command if there's at least 1 object loadedsqlsequencereset
mgt commandSQLite
supports_sequence_reset
setsql_flush()
defined - usesDELETE FROM <table>
sequence_reset_by_name_sql()
sequence_reset_by_name_sql()
defined - usingUPDATE sqlite_sequence …
sequence_reset_sql()
not definedPostgreSQL
supports_sequence_reset
setsql_flush()
defined - usesTRUCATE <table1>, <table2>, …
, addsRESTART IDENTITY
if resetting sequencessequence_reset_by_name_sql()
defined - usingSELECT setval(pg_get_serial_sequence(<table>, <column>), 1, false)
sequence_reset_sql()
defined withSELECT setval(pg_get_serial_sequence(<table>, <column>), coalesce(max(<column>), 1), max(<column>) IS NOT NULL) FROM <table>;
MySQL
supports_sequence_reset
setsql_flush()
defined - resets sequences by usingTRUNCATE
instead ofDELETE
sequence_reset_by_name_sql()
defined usingALTER TABLE <table> AUTO_INCREMENT = 1
sequence_reset_sql()
not definedOracle
supports_sequence_reset
not setsql_flush()
defined - iterates over tables, disables constraints thenTRUNCATE
, then re-enabling constraintssequence_reset_by_name_sql()
sequence_reset_by_name_sql()
defined using custom PL/SQLsequence_reset_sql()
defined using custom PL/SQLSome notes about resetting sequences on Oracle
ALTER SEQUENCE
, one needs to specify the start byALTER TABLE … MODIFY id GENERATED BY DEFAULT ON NULL AS IDENTITY START WITH …
LIMIT VALUE
to specify the max valueLIMIT VALUE
equivalent so we need to retain the existingSELECT MAX(…)
statement to retrieve the max valueAdditional notes
nextvalue
until it reached the maxALTER TABLE … MODIFY
statement should probably get the proper type info from schema?