D
D
DTPlayer2020-06-20 16:28:14
Python
DTPlayer, 2020-06-20 16:28:14

How to solve the problem with the database and ref system?

main.py

from models import *
import telebot

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


@bot.message_handler(commands=['start'])
def start(message):
    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))


@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_user(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)


DB:
CREATE TABLE users (
    id          INTEGER       NOT NULL ON CONFLICT REPLACE
                              PRIMARY KEY ASC,
    user_id     INT           NOT NULL
                              UNIQUE ON CONFLICT IGNORE,
    user_login  VARCHAR (200),
    user_name   TEXT,
    second_name TEXT,
    clock       DATE,
    parent_id   INTEGER, 
    refferer_id INTEGER
);

Gives an error message:
peewee.OperationalError: no such column: t1.ref

What other block in the database should be created

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-06-20
@SoreMix

>no ref column

CREATE TABLE users (
id INTEGER NOT NULL ON CONFLICT REPLACE
PRIMARY KEY ASC,
user_id INT NOT NULL
UNIQUE ON CONFLICT IGNORE,
user_login VARCHAR(200),
user_name TEXT,
second_name TEXT,
clock DATE,
parent_id INTEGER,
refferer_id INTEGER
);

What else is there to say

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question