0

I am working on a delete request. All of my functions are working, but especially this one is not. Actually, it runs, and using my insomnia looks like the row was deleted, but when I get all rows it is still there.

This belongs to my init.py from model folder:

class DatabaseConnector:
    @classmethod
    def get_conn_cur(cls):
        cls.conn = psycopg2.connect(**configs)
        cls.cur = cls.conn.cursor()

    @classmethod
    def commit_and_close(cls):
        cls.conn.commit()
        cls.cur.close()
        cls.conn.close()

This belongs to my anime_model.py folder:

class Anime(DatabaseConnector):

    def __init__(self, anime: string, released_date: string, seasons: int) -> None:
        self.anime = anime.lower().title() 
        self.released_date = released_date
        self.seasons = seasons

    @classmethod
    def remove_an_anime(cls, anime_id: int):
        cls.get_conn_cur()
        cls.create_a_table()
        query = f"DELETE FROM animes WHERE id={anime_id} RETURNING *";
        cls.cur.execute(query)
        anime = cls.cur.fetchall()
        cls.cur.close()
        cls.conn.close()
        return anime

It belongs to my controller folder, anime_controller.py file:

def delete_an_anime(anime_id):

    anime_to_delete = Anime.remove_an_anime(anime_id)
    
    if not anime_to_delete:
        return {"error": f"id {anime_id} not found"}, HTTPStatus.NOT_FOUND
    
    serialized_anime = dict(zip(anime_columns, anime_to_delete))
    return serialized_anime, HTTPStatus.ACCEPTED
  • 1
    Try committing your delete before re-fetching the data. – BoarGules Mar 29 '22 at 14:48
  • 3
    Not related to the question, but never do this: `f"DELETE FROM animes WHERE id={anime_id} RETURNING *"`. That code is open to SQL injection. – zvone Mar 29 '22 at 15:30
  • Agreed with @zvone. Interestingly, psycopg2 supported named params and can use the `anime_id` alias. – Parfait Mar 29 '22 at 15:46

0 Answers0