1 year ago

#349952

test-img

user17841947

Why is the DELETE statement using psycopg is not working?

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

python

postgresql

flask

sql-delete

0 Answers

Your Answer

Accepted video resources