diff --git a/tests/conftest.py b/tests/conftest.py index b8733e0..be692d5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -166,6 +166,17 @@ def _reset_db_after_integration(request, _truncate_engine): rows = (_SEED_SNAPSHOT or {}).get(t.name) if rows: conn.execute(t.insert(), rows) + # Restored rows carry their explicit ids while RESTART + # IDENTITY reset the sequence to 1, so the next ORM insert + # would collide on the PK (bitten by migration 0075's seeded + # system tags: every Tag insert failed with pk_tag id=1). + # Resync any serial id sequence past the restored rows. + if "id" in t.c: + conn.exec_driver_sql( + f"SELECT setval(pg_get_serial_sequence('{t.name}', 'id'), " + f"(SELECT COALESCE(MAX(id), 1) FROM {t.name})) " + f"WHERE pg_get_serial_sequence('{t.name}', 'id') IS NOT NULL" + ) @pytest_asyncio.fixture(autouse=True)