@progerlox
мне 14 лет и моя цель стать миллионером

Выводит ошибку sqlite3.OperationalError: no such column: t1.user_id. Что делать?

Делаю реферальную ссылку в телеграм боте через telebot. Создал таблицу, ошибка что не может найти колонку, еще с этим t1. хоть в синтаксисе названий колонок нельзя поставить точку. Что делать???

файл main.py

import telebot
from models import *
import sqlite3
from telebot import types


bot = telebot.TeleBot('токен')
ref_link = 'https://telegram.me/{}?start={}'

conn = sqlite3.connect('users.db')
cur = conn.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS users(
   user_id TEXT,
   lname TEXT,
   gender TEXT);
""")
conn.commit()

@bot.message_handler(commands=['start'])
def start_message(message):
    print('1')
    user_id = message.chat.id
    splited = message.text.split()
    if not Users.user_exists(user_id):
        Users.create_user(user_id)
        if len(splited) == 2:
            Users.increase_ref_count(splited[1])
    bot.reply_to(message, text='hello')


@bot.message_handler(commands=['ref'])
def get_my_ref(message):
    bot_name = bot.get_me().username
    bot.reply_to(message, text=ref_link.format(bot_name, message.chat.id))
    ois = ref_link.format(bot_name, message.chat.id)
    bot.send_message(message.chat.id, ois)


@bot.message_handler(commands=['ref_count'])
def get_my_refs(message):
    count = Users.get_ref_count(message.chat.id)
    bot.reply_to(message, text=f'Count: {count}')


if __name__ == '__main__':
    bot.polling(none_stop=True)


Файл models.py

from peewee import *

db = SqliteDatabase('users.db')


class BaseModel(Model):
    class Meta:
        database = db


class Users(BaseModel):
    user_id = IntegerField(unique=True)
    ref = IntegerField(default=0)

    @classmethod
    def get_user(cls, user_id):
        return cls.get(user_id == user_id)

    @classmethod
    def get_ref_count(cls, user_id):
        return cls.get(Users.user_id == user_id).ref

    @classmethod
    def increase_ref_count(cls, user_id):
        user = cls.get_user(user_id)
        user.ref += 1
        user.save()

    @classmethod
    def user_exists(cls, user_id):
        query = cls().select().where(cls.user_id == user_id)
        return query.exists()

    @classmethod
    def create_user(cls, user_id):
        user, created = cls.get_or_create(user_id=user_id)


Все ошибки

Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 3160, in execute_sql
cursor.execute(sql, params or ())
sqlite3.OperationalError: no such column: t1.user_id

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\User\pythonProject156\main.py", line 91, in
bot.polling(none_stop=True)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 455, in polling
self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 513, in __threaded_polling
raise e
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 479, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\util.py", line 118, in raise_exceptions
raise self.exception_info
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\util.py", line 69, in run
task(*args, **kwargs)
File "C:\Users\User\pythonProject156\main.py", line 29, in start_message
if not Users.user_exists(user_id):
File "C:\Users\User\pythonProject156\models.py", line 32, in user_exists
return query.exists()
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 1911, in inner
return method(self, database, *args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 2196, in exists
return bool(clone.scalar())
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 1911, in inner
return method(self, database, *args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 2174, in scalar
row = self.tuples().peek(database)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 1911, in inner
return method(self, database, *args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 2161, in peek
rows = self.execute(database)[:n]
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 1911, in inner
return method(self, database, *args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 1982, in execute
return self._execute(database)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 2155, in _execute
cursor = database.execute(self)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 3173, in execute
return self.execute_sql(sql, params, commit=commit)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 3167, in execute_sql
self.commit()
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 2933, in __exit__
reraise(new_type, new_type(exc_value, *exc_args), traceback)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 191, in reraise
raise value.with_traceback(tb)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\peewee.py", line 3160, in execute_sql
cursor.execute(sql, params or ())
peewee.OperationalError: no such column: t1.user_id
  • Вопрос задан
  • 628 просмотров
Пригласить эксперта
Ответы на вопрос 1
ErVinTract
@ErVinTract
Жизнерадостный человек
Ошибка у тебя в
@classmethod
    def user_exists(cls, user_id):
        query = cls().select().where(cls.user_id == user_id) # В этой строке, скорее всего cls не верный
        return query.exists()
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы