Запрос INSERT INTO выполняется не правильно в asyncpg
Я пытаюсь выполнить запрос с вставкой необходимой информации для бота, так как я новичёк, большая часть кода не моя. И поэтому я не могу просто вырезать этот запрос и сделать его вручную.
Самое странное что в pgAdmin4 этот запрос выполняется без ошибок, однако в коде он выдаёт ошибку
asyncpg.exceptions.NotNullViolationError: значение NULL в столбце "numip" отношения "servers" нарушает ограничение NOT NULL
DETAIL: Ошибочная строка содержит (0, null, null, null, null).
Собственно сам код (обрезал как мог). Проблемное место на строке 35, или же там где написано
conn.execute(""" INSERT INTO servers (id, numip, maxpl) VALUES (1, '127.0.0.1', 0) ON CONFLICT DO NOTHING ;""")
from discord.ext import commands, tasks
import discord
import asyncpg
bot_intents = discord.Intents.default()
bot_intents.members = True
bot = commands.Bot(
command_prefix='',
description="описание",
case_insensitive=False,
help_command=None,
status=discord.Status.invisible,
intents=bot_intents,
fetch_offline_members=True
)
bot.ready_for_commands = False
async def create_pool():
"""Создает таблицы в бд, если они уже есть - загружает их"""
bot.pool = await asyncpg.create_pool('postgres://test:123123123@localhost:5432/postgres')
async with bot.pool.acquire() as conn:
await conn.execute("""
CREATE TABLE IF NOT EXISTS servers (
id BIGINT PRIMARY KEY NOT NULL DEFAULT 0,
domen TEXT,
numip CIDR NOT NULL DEFAULT '127.0.0.1',
alias TEXT,
maxpl INTEGER NOT NULL DEFAULT 0
);""")
conn.execute("""
INSERT INTO servers (id, numip, maxpl)
VALUES (1, '127.0.0.1', 0) ON CONFLICT DO NOTHING
;""")
query = await conn.fetch("SELECT * FROM servers;")
bot.sunpings = {}
bot.servers = {}
for i in query:
bot.servers.update({i.get("id"): dict(i)})
@bot.event
async def on_ready():
await create_pool()
print("\nЗапуск успешен")
update_db.start()
bot.ready_for_commands = True
bot.started_at = datetime.datetime.utcnow()
bot.app_info = await bot.application_info()
@tasks.loop(minutes=5, loop=bot.loop)
async def update_db():
"""Обновляет ДБ каждые 5 минут"""
async with bot.pool.acquire() as conn:
await conn.execute("""
INSERT INTO servers
(id)
VALUES {}
ON CONFLICT
DO NOTHING
;""".format(", ".join([f"({u})" for u in bot.servers])))
for data in bot.servers.copy().values():
await conn.execute("""
UPDATE servers
SET total = {}
WHERE id = {}
;""".format(data["total"], data["id"]))
try:
bot.loop.run_until_complete(bot.start('ODM0MTc5Mzk5NDIyNzA1NzI0.YH9IGw.iB285ySqDOMlzFVDnc-NICFWzT4'))
except KeyboardInterrupt: pass