diff --git a/olgram/migrations/models/1_20210909220512_update.sql b/olgram/migrations/models/1_20210909220512_update.sql deleted file mode 100644 index 646bf76..0000000 --- a/olgram/migrations/models/1_20210909220512_update.sql +++ /dev/null @@ -1,5 +0,0 @@ --- upgrade -- -CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; -ALTER TABLE "bot" ADD "code" UUID NOT NULL DEFAULT uuid_generate_v4(); --- downgrade -- -ALTER TABLE "bot" DROP COLUMN "code"; diff --git a/olgram/migrations/models/0_20210711121349_init.sql b/olgram/migrations/models/1_20210911011051_None.sql similarity index 84% rename from olgram/migrations/models/0_20210711121349_init.sql rename to olgram/migrations/models/1_20210911011051_None.sql index e78e993..ed1cf82 100644 --- a/olgram/migrations/models/0_20210711121349_init.sql +++ b/olgram/migrations/models/1_20210911011051_None.sql @@ -14,10 +14,12 @@ CREATE TABLE IF NOT EXISTS "bot" ( "id" SERIAL NOT NULL PRIMARY KEY, "token" VARCHAR(50) NOT NULL UNIQUE, "name" VARCHAR(33) NOT NULL, + "code" UUID NOT NULL, "start_text" TEXT NOT NULL, - "group_chat_id" INT REFERENCES "group_chat" ("id") ON DELETE SET NULL, + "group_chat_id" INT REFERENCES "group_chat" ("id") ON DELETE CASCADE, "owner_id" INT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE ); +CREATE INDEX IF NOT EXISTS "idx_bot_code_a43015" ON "bot" ("code"); CREATE TABLE IF NOT EXISTS "aerich" ( "id" SERIAL NOT NULL PRIMARY KEY, "version" VARCHAR(255) NOT NULL, @@ -25,6 +27,6 @@ CREATE TABLE IF NOT EXISTS "aerich" ( "content" JSONB NOT NULL ); CREATE TABLE IF NOT EXISTS "bot_group_chat" ( - "bot_id" INT NOT NULL REFERENCES "bot" ("id") ON DELETE SET NULL, - "groupchat_id" INT NOT NULL REFERENCES "group_chat" ("id") ON DELETE SET NULL + "bot_id" INT NOT NULL REFERENCES "bot" ("id") ON DELETE CASCADE, + "groupchat_id" INT NOT NULL REFERENCES "group_chat" ("id") ON DELETE CASCADE ); diff --git a/olgram/models/models.py b/olgram/models/models.py index 735196e..16bd2fa 100644 --- a/olgram/models/models.py +++ b/olgram/models/models.py @@ -15,9 +15,10 @@ class Bot(Model): Напишите ваш вопрос и мы ответим вам в ближайшее время. """)) - group_chats = fields.ManyToManyField("models.GroupChat", related_name="bots", on_delete=fields.relational.SET_NULL) + group_chats = fields.ManyToManyField("models.GroupChat", related_name="bots", on_delete=fields.relational.CASCADE, + null=True) group_chat = fields.ForeignKeyField("models.GroupChat", related_name="active_bots", - on_delete=fields.relational.SET_NULL, + on_delete=fields.relational.CASCADE, null=True) async def super_chat_id(self):