I am working on a simple app using the remotive.io api where a user can sign up/login and search for jobs in a specific field. The user is able to favorite/save jobs that are of interest. I've implemented the correct logic to save a favorited job to the database.
My crummy logic for getting the job data aside, I am having trouble deleting a SavedJob instance from the db. In my 'else' statement, if I print the job_id I am getting the correct job_id from the request.
When I try to
job_id=request.json["saved_job_id"]
saved_job = SavedJob.query.get(job_id)
If I try to print the saved_job, I am returned
None
Since I am able to get the job_id correctly from the request, I don't understand why my database query is not returning the match of that job_id.
Here is my code block as I would like to have it function:
@app.route('/api/saved-jobs', methods=["POST", "GET", "DELETE"])
def list_saved_jobs():
exists = db.session.query(db.exists().where(SavedJob.job_id == request.json["saved_job_id"])).scalar()
new_saved_job = SavedJob(job_id=request.json["saved_job_id"], user_id=request.json["user_id"], job_title=request.json["job_title"], company_name=request.json["company_name"])
if exists == False:
db.session.add(new_saved_job)
db.session.commit()
return ('', 201)
else:
job_id=request.json["saved_job_id"]
saved_job = SavedJob.query.get(job_id)
db.session.delete(saved_job)
db.session.commit()
return ('', 200)
Please keep in mind that I am fairly early on in my programming journey. Any constructive criticism is gladly accepted. Thank you.
question from:
https://stackoverflow.com/questions/66049015/flask-sqlalchemy-model-query-returning-none 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…