简体   繁体   English

Flask-SQLAlchemy model 查询返回无

[英]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.我正在使用 remotive.io api 开发一个简单的应用程序,用户可以在其中注册/登录并搜索特定领域的工作。 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.我将作业数据放在一边的糟糕逻辑,我无法从数据库中删除SavedJob 实例。 In my 'else' statement, if I print the job_id I am getting the correct job_id from the request.在我的“else”语句中,如果我打印 job_id,我将从请求中获得正确的 job_id。

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如果我尝试打印 saved_job,我会被退回

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.由于我能够从请求中正确获取 job_id,我不明白为什么我的数据库查询没有返回该 job_id 的匹配项。

Here is my code block as I would like to have it function:这是我的代码块,因为我想拥有它 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.谢谢你。

I was able to solve this by using the filter_by filter on my query.我能够通过在我的查询中使用 filter_by 过滤器来解决这个问题。 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.我还使用and运算符(逗号)指定了 user_id=g.user.id 将多个约束链接在一起,以解决不同用户保存同一个作业的问题。

I also had to use the and operator when defining "exists" to check if the current user matched.在定义“存在”时,我还必须使用and运算符来检查当前用户是否匹配。 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.我将在某个时候返回 go 并清理这个烂摊子。 I hope this helps someone in the future who might run into a similar problem.我希望这对将来可能遇到类似问题的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM