the Like api is supposed to check if the recipe has been liked before. If yes, it should delete and if no, it should add like to db but I get an error.
@app.route("/like/<int:id>", methods=['GET'])
@jwt_required()
def like(id):
current_user = get_jwt_identity()
if not Recipe.select().where(Recipe.id == id).exists():
return jsonify({'Status': 'Unsuccessful', 'Message': 'Recipe ID does not Exist'}), 400
like = Like.select().where(Like.recipe_id == id) & (Like.poster_id == current_user)
if like:
like = Like.delete().where(Like.recipe_id == id) & (Like.poster_id == current_user)
data = like.execute()
return jsonify({'Status': data, 'Message': 'Like Deleted'}), 204
new_like = Like.create(recipe_id = id , poster_id=current_user )
return jsonify({'Status': 'Successful', 'Message': 'You have liked a recipe'}), 200
this is the error:
File "/Users/user/PythonProject/venv/lib/python3.8/site-packages/pymysql/protocol.py", line 221, in raise_for_error
err.raise_mysql_exception(self._data)
File "/Users/user/PythonProject/venv/lib/python3.8/site-packages/pymysql/err.py", line 143, in raise_mysql_exception
raise errorclass(errno, errval)
peewee.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTERSECT ((`t2`.`poster_id` = 2))' at line 1")
Here is the dislike API
@app.route("/dislike/<int:id>", methods=['GET'])
@jwt_required()
def dislike(id):
current_user = get_jwt_identity()
if not Recipe.select().where(Recipe.id == id).exists():
return jsonify({'Status': 'Unsuccessful', 'Message': 'Recipe ID does not Exist'}), 400
like = Like.select().where(Like.recipe_id == id)
dislike = Dislike.select().where(Dislike.recipe_id == id)
if dislike:
remove_dislike = Dislike.delete().where(Dislike.poster_id == current_user)
data = remove_dislike.execute()
return jsonify({'Status': data, 'Message': 'Dislike Deleted'}), 204
elif like:
remove_like = Like.delete().where(Like.poster_id == current_user) & (Like.recipe_id == id)
data = remove_like.execute()
return jsonify({'Status': data, 'Message': 'Like Deleted'}), 204
else:
new_dislike = Dislike.create(recipe_id = id , poster_id=current_user )
return jsonify({'Status': 'Successful', 'Message': 'You have disliked a recipe'}), 200
The second API is to dislike a recipe. i need this API to check first if the API has been like and delete it, if not check if it has been disliked, then delete else add dislike to db but it just return 1 and nothing happens in the db. I cant seem to figure out what i am doing wrong.
Here is what my model looks like;
class Like(BaseModel):
id = PrimaryKeyField(primary_key=True)
recipe_id = ForeignKeyField(Recipe, backref='comment', lazy_load=False)
post_date = DateTimeField(constraints=[SQL('DEFAULT CURRENT_TIMESTAMP')])
poster_id = ForeignKeyField(Users, backref='like', lazy_load=False)
class Dislike(BaseModel):
id = PrimaryKeyField(primary_key=True)
recipe_id = ForeignKeyField(Recipe, backref='comment', lazy_load=False)
post_date = DateTimeField(constraints=[SQL('DEFAULT CURRENT_TIMESTAMP')])
poster_id = ForeignKeyField(Users, backref='dislike', lazy_load=False)
I can't check it but as for me you have
( )
in wrong places and& (...)
is outsidewhere(...)
and this makes problem.You have
& (...)
outsidewhere(...)
but it should be inside
where(...)
Eventually it could work with two
where
And the same is in other places.
Peewee doc: Query operators