简体   繁体   English

Flask sqlalchemy PUT请求,如何从关系表中查询两个动态外键ID

[英]Flask sqlalchemy PUT request, How query for two dynamic foreign key ids from a relationship table

I'm trying to update data in the front end using React/Redux from a PUT request in a flask/sqlalchemy/postgres backend. 我正在尝试使用来自flask / sqlalchemy / postgres后端中的PUT请求的React / Redux来更新前端的数据。 My problem is that my endpoint is only able to be updated by the id, which is useless to the frontend. 我的问题是我的端点只能通过id更新,这对前端没用。 The reason this is, is because the data being updated is from a relationship class that only exists to join two other classes. 这是因为正在更新的数据来自一个仅存在以加入另外两个类的关系类。 What I need to do is figure out how to query and return by user_id and country_id rather than id. 我需要做的是弄清楚如何通过user_id和country_id而不是id来查询和返回。 By the way, I'm testing locally with Postman. 顺便说一下,我正在和Postman一起在当地进行测试。 The models for the join class(table) are as follows 连接类(表)的模型如下

class users_countries_join(db.Model):
    id = db.Column(Integer, autoincrement=True, primary_key=True)
    user_id = db.Column(Integer, ForeignKey(users.id), nullable=False)
    country_id = db.Column(Integer, ForeignKey(countries.id), nullable=False)
    status = db.Column(Integer, nullable=False)
    notes = db.Column(TEXT, nullable=True)
    user = db.relationship('users', backref='user_countries')
    country = db.relationship('countries', backref='travelers')

    def __init__(self, user_id, country_id, status, notes):
        self.user_id = user_id
        self.country_id = country_id
        self.status = status
        self.notes = notes

    def __repr__(self):
        return '<{}>' % self.__name__

The code for the endpoint is: 端点的代码是:

@app.route('/api/mapview/<int:user_id>/<int:country_id>/<int:id>', methods=['PUT'])
def update_mapView_data(user_id, country_id, id):
   user_country = users_countries_join.query.get(id)
   user_country.user_id = request.json[‘user_id’]
   user_country.country_id = request.json[‘country_id’]
   user_country.status = request.json[‘status’]
   user_country.notes = request.json[‘notes’]
    db.session.commit()
    return user_country_schema.jsonify(user_country)

I've tried to change the endpoint to the code, below, but that doesn't work. 我试图将端点更改为下面的代码,但这不起作用。 In theory, that's the sort of update i need though. 从理论上讲,这是我需要的那种更新。 One that updated by accessing the data via the user_id and country_id rather than the id of the class. 通过user_id和country_id而不是类的id访问数据来更新的一个。 I'm beginning to think that id is unnecessary to begin with. 我开始认为id是不必要的。

@app.route('/api/mapview/<int:user_id>/<int:country_id>', methods=['PUT'])
def update_mapView_data(user_id, country_id):
   user_country = users_countries_join.query.get(user_id, country_id)
   user_country.user_id = request.json[‘user_id’]
   user_country.country_id = request.json[‘country_id’]
   user_country.status = request.json[‘status’]
   user_country.notes = request.json[‘notes’]
    db.session.commit()
    return user_country_schema.jsonify(user_country)

Can someone point me in the right direction in how to do this? 有人能指出我如何做到这一点的正确方向? I'm hoping I'm just making this more difficult than it has to be. 我希望我只是让它变得比以往更难。

You can use filter_by() method to filter rows. 您可以使用filter_by()方法过滤行。 For example, if you want to get first user_country using user_id and country_id you can use following query: 例如,如果要使用user_id和country_id获取第一个user_country,可以使用以下查询:

user_country = users_countries_join.query.filter_by(user_id=user_id, country_id=country_id).first()

If you have more than one database row with same user_id or country_id: 如果您有多个具有相同user_id或country_id的数据库行:

user_country = users_countries_join.query.filter_by(user_id=user_id, country_id=country_id).all()

In your example I guess that you want to get the only one unique row so you can use the first query. 在您的示例中,我猜您想要获得唯一一个唯一行,以便您可以使用第一个查询。 Also, after you change model properties values you have to add that change to the db.session: 此外,更改模型属性值后,您必须将该更改添加到db.session:

db.session.add(user_country)

and commit change to the database: 并提交对数据库的更改:

db.session.commit()

I want also to note that you should use camel case when you declaring python class name like UserCountry or only Cuntry and for joining two models(tables) you shouldn't define same attributes that you have in User class or Country class. 我还要注意,当您声明像UserCountry或仅Cuntry这样的python类名时应该使用camel case,并且为了连接两个模型(表),您不应该定义User类或Country类中的相同属性。 In my opinion that way of joining two tables is redundant. 在我看来,加入两个表的方式是多余的。

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

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