From 3c9548540ce0d074c7e9a634a3daa7b4f5834492 Mon Sep 17 00:00:00 2001 From: Drobysh Date: Fri, 16 Oct 2026 11:58:59 +0000 Subject: [PATCH] =?UTF-8?q?[Eva]=20B5.3:=20database.py=20=E2=80=94=20UNIQU?= =?UTF-8?q?E(sentence=5Fnumber)=20+=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D1=83?= =?UTF-8?q?=D0=BC=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/database.py b/database.py index c62b308..dad15c5 100644 --- a/database.py +++ b/database.py @@ -258,20 +258,31 @@ async def init_db(): ) """) - # ============ Убираем старый UNIQUE constraint с sentences.sentence_number ============ + # ============ Убираем составной UNIQUE (level, sentence_number) ============ await conn.execute(""" - ALTER TABLE sentences DROP CONSTRAINT IF EXISTS sentences_sentence_number_key + ALTER TABLE sentences DROP CONSTRAINT IF EXISTS sentences_sentence_number_unique """) - # ============ Добавляем составной UNIQUE (level, sentence_number) ============ + # ============ Пересчёт номеров: единый пул 1..N ============ + await conn.execute(""" + UPDATE sentences s + SET sentence_number = sub.rn + FROM ( + SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS rn + FROM sentences + ) sub + WHERE s.id = sub.id + """) + + # ============ Добавляем UNIQUE (sentence_number) ============ await conn.execute(""" DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_constraint - WHERE conname = 'sentences_sentence_number_unique' + WHERE conname = 'sentences_sentence_number_key' ) THEN - ALTER TABLE sentences ADD CONSTRAINT sentences_sentence_number_unique UNIQUE (level, sentence_number); + ALTER TABLE sentences ADD CONSTRAINT sentences_sentence_number_key UNIQUE (sentence_number); END IF; END $$; """)