Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

python - Flask-SQLAlchemy model query returning None

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was able to solve this by using the filter_by filter on my query. I also specified the user_id=g.user.id using the and operator (comma) to chain together multiple constraints to solve different users saving the same job.

I also had to use the and operator when defining "exists" to check if the current user matched. The whole block is looking like this now and seems to work so far:

@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() and db.session.query(db.exists().where(SavedJob.user_id == g.user.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:
        saved_job_id=request.json["saved_job_id"]
        saved_job = SavedJob.query.filter_by(job_id=saved_job_id, user_id=g.user.id).first()
        db.session.delete(saved_job)
        db.session.commit()
        return ('', 200)

I'm going to go back and clean this mess up as well at some point! I hope this helps someone in the future who might run into a similar problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...