Snowflake-Labs / django-snowflake

MIT License
59 stars 15 forks source link

Add support for resetting autoincrement sequences #40

Open timgraham opened 2 years ago

timgraham commented 2 years ago

When loading fixtures that have primary key values of autoincrement columns specified, Snowflake doesn't automatically update the sequence to the next available value. For example, after loading fixtures with pk=1 and pk=2 into a new table, the next object will be created with pk=1 (duplicating an existing value) instead of pk=3. PostgreSQL has the same issue and Django's DatabaseOperations.sequence_reset_sql() method is a hook to generate SQL that updates the next value of the sequence for each table:

SELECT setval(pg_get_serial_sequence(<table>, <column>), coalesce(max(<column>)), 1), max(column) IS NOT null)
FROM <table>

Since Snowflake's ALTER SEQUENCE doesn't provide a way to set the next value of the sequence, I'm not sure if this is feasible.