-1

I'm developing a telegram bot. So i have three tables in my database:

  1. Users - stores information about users. fields:
    user: stores user's telegram id. it needs to assign a record to the user
    first name, last name, etc
  2. Notes - stores user's notes. fields:
    user: foreign key related to field user in table Users.
    id: id of record
    text of note, date created, etc
  3. Records - stores user's reminders. it is similar to the table Notes. fields:
    user: foreign key related to field user in table Users.
    id: id of record
    text of record, date, etc

the thing is when you ask the bot to show your records (from Notes or Records) it sends you a message with the text: "send me /del{id} to delete this record", where id is record's id. Here is the problem: if there are 50 rows in the table and somebody started to use my bot, when he creates the 1st record then bot sends him a message "send me /del51 to delete this record".

so here is my question: is there any possibility to make record's id equal to quantity of user's records in the current table?

I need someting like that:

user text created id
1234 qwerty 15.12.2022 1
1234 qwerty1 17.12.2022 2
1234 qwerty2 18.12.2022 3
1234 qwerty3 18.12.2022 4
456 xyzr1 16.12.2022 1
456 xyzr2 17.12.2022 2
456 xyzr3 18.12.2022 3
456 xyzr4 19.12.2022 4
pizhlo
  • 107
  • 6
  • 1
    Well, yeah, just get the `MAX()` value of that user and add 1. I think. It's hard to follow your question – roganjosh Dec 17 '22 at 21:02
  • yah your question isn't clear at all... can you please try to explain a little more – Alexander Dec 17 '22 at 21:14
  • @Alexander ok i'll try to explain again. imagine that my bot has 100 rows in the table Notes. you started to use my bot. so you create a new note (this note is the first for you(!)). i want bot to say you: "send me /del1 to delete this record" when it shows you your record. but according to it's current behavior it will say: "send me /del101 to delete this record" because there are 100 more lines in it besides yours. so I want my bot to say: "send me /del1 to delete this record" because this is the 1st record for you(!) and no matter that this is the 101st record in the table – pizhlo Dec 17 '22 at 22:15
  • @roganjosh ok i'll try to explain again. imagine that my bot has 100 rows in the table Notes. you started to use my bot. so you create a new note (this note is the first for you(!)). i want bot to say you: "send me /del1 to delete this record" when it shows you your record. but according to it's current behavior it will say: "send me /del101 to delete this record" because there are 100 more lines in it besides yours. so I want my bot to say: "send me /del1 to delete this record" because this is the 1st record for you(!) and no matter that this is the 101st record in the table – pizhlo Dec 17 '22 at 22:16
  • @pizhlo [edit] your question to include that information – Alexander Dec 17 '22 at 22:46
  • The answer to your question though is simple... make a new column in your database that holds the note number for each user. You can use the username and the note number as a combined unique id for each note on top of the already unique ID created by the general not id number that is for all notes in the database. And when you send the message use that number instead of the general one – Alexander Dec 17 '22 at 22:50

1 Answers1

0

my solution is:

  1. count how much has current user rows in the table
  2. add one so this is id
def get_records(table: str, user_id: int) -> list:
    cursor.execute(f'SELECT * FROM {table} WHERE user = {user_id}')
    records = cursor.fetchall()
    return records

def generate_id(table: str, user_id: int) -> int:
    records = get_records(table, user_id)
    return len(records) + 1
pizhlo
  • 107
  • 6
  • 1
    This is broken in multiple ways. `return len(records) + 1` good luck - once you start deleting records, the counter for the ID will still keep incrementing, but the length of the results returned will decrement, so now it's completely out of sync. Secondly, `f'SELECT * FROM {table} WHERE user = {user_id}'` is open to SQL Injection - you should be using parameterization – roganjosh Dec 18 '22 at 17:19