Flask RESTful-查询SQLalchemy返回两个列表

时间:2019-01-14 12:51:53

标签: python sqlalchemy

我是python的初学者,我目前正在对餐厅进行api分组。

麻烦的是,当我加入两个餐桌餐厅和地址时,查询SQLAlchemy向我发送了2个列表,但是我想合并他的列表

我了解到:https://docs.sqlalchemy.org/en/rel_1_1/orm/basic_relationships.html#many-to-many

我的两个班级:餐馆和Adresse

restaurant_adresse_association = db.Table(
   'restaurant_adresse',
    db.Column('restaurant_id', db.Integer, ForeignKey('restaurants.id')),
    db.Column('adresse_id', db.Integer, ForeignKey('adresse.id'))
)

 class Restaurants(db.Model):
     __tablename__ = 'restaurants'
   id = db.Column(db.Integer, primary_key=True)
   nom = db.Column(db.String(255))
   description = db.Column(db.String(255))
   creation = db.Column(db.DateTime)
   heure_ouverture = db.Column(db.DateTime)
   heure_fermeture = db.Column(db.DateTime)
   url_photo = db.Column(db.String(255))
   rang = db.Column(db.Integer)
   adresse = db.relationship('Adresse',secondary=restaurant_adresse_association)


 class Adresse(db.Model):
    __tablename__ = 'adresse'
    id = db.Column(db.Integer, primary_key=True)
    ville = db.Column(db.String(255))
    code_postal = db.Column(db.String(255))
    rue = db.Column(db.String(255))
    restaurant = db.relationship('Restaurants', secondary=restaurant_adresse_association)
    longitude = db.Column(db.Float)
    latitude = db.Column(db.Float)

Restaurant.py:

champs_restaurant = {
'id': fields.Integer(attribute='id'),
'name': fields.String(attribute='nom'),
'city': fields.String(attribute='ville'),
'address': fields.String(attribute='rue'),
'postal code': fields.String(attribute='code_postal'),
'description': fields.String,
'opening time': fields.String(attribute='heure_ouverture'),
'closing time': fields.String(attribute='heure_fermeture'),
'picture': fields.String(attribute='url_photo'),
'rank': fields.Integer(attribute='rang')
 }

@marshal_with(champs_restaurant)
def get(self):
    resto = session.query(Restaurants, Adresse).join(Adresse, Restaurants.adresse).all()
    return resto, 201

结果:

[
[
    {
        "id": 1,
        "name": "Hugiz",
        "city": null,
        "address": null,
        "postal code": null,
        "description": "Fastfood",
        "opening time": "9:00",
        "closing time": "18:00",
        "picture": null,
        "rank": 4
    },
    {
        "id": 1,
        "name": null,
        "city": "Paris",
        "address": "1-3 Rue de Savies",
        "postal code": "75020",
        "description": null,
        "opening time": null,
        "closing time": null,
        "picture": null,
        "rank": 0
    }
],
[
    {
        "id": 2,
        "name": "estampille",
        "city": null,
        "address": null,
        "postal code": null,
        "description": "Pizza",
        "opening time": "9:00",
        "closing time": "18:00",
        "picture": null,
        "rank": 4
    },
    {
        "id": 2,
        "name": null,
        "city": "Rouen",
        "address": "1 Rue Thomas Becket",
        "postal code": "76130",
        "description": null,
        "opening time": null,
        "closing time": null,
        "picture": null,
        "rank": 0
    }
]

测试:

@marshal_with(champs_restaurant)
def get(self):
    resto = session.query(Restaurants).join(Adresse).all()
    return resto, 201

结果:

sqlalchemy.exc.InvalidRequestError: Could not find a FROM clause to join from.  Tried joining to <class 'source.Restaurant.modèle.modele_restaurant.Adresse'>, but got: Can't find any foreign key relationships between 'restaurants' and 'adresse'.

测试:

@marshal_with(champs_restaurant)
def get(self):
    resto = session.query(Restaurants).join(Adresse, Restaurants.adresse).all()
    return resto, 201

结果:

[
    {
        "id": 1,
        "name": "Hugiz",
        "city": null,
        "address": null,
        "postal code": null,
        "description": "Fastfood",
        "opening time": "9:00",
        "closing time": "18:00",
        "picture": null,
        "rank": 4
    }, 
    {
        "id": 2,
        "name": "estampille",
        "city": null,
        "address": null,
        "postal code": null,
        "description": "Pizza",
        "opening time": "9:00",
        "closing time": "18:00",
        "picture": null,
        "rank": 4
    }
]

预期结果:

[
    {
        "id": 1,
        "name": "Hugiz",
        "city": "Paris",
        "address": "1-3 Rue de Savies",
        "postal code": "75020",
        "description": "Fastfood",
        "opening time": "9:00",
        "closing time": "18:00",
        "picture": null,
        "rank": 4
    },
    {
        "id": 2,
        "name": "estampille",
        "city": "Rouen",
        "address": "1 Rue Thomas Becket",
        "postal code": "76130",
        "description": "Pizza",
        "opening time": "9:00",
        "closing time": "18:00",
        "picture": null,
        "rank": 4
    }
]

1 个答案:

答案 0 :(得分:1)

您要查询两个表:

session.query(Restaurants, Adresse)

这基本上等同于像这样的SQL语句

SELECT * FROM restaurants, adresse;

在表之间创建一个implicit cross join,这可能不是您想要的。在没有看到champs_restaurant的情况下,很难确切地说出发生了什么,但是似乎正在尝试将包含两个表中的列的结果填充为用于合并结果的JSON格式。

在SQLAlchemy ORM中配置关系的部分要点是您可以在表上进行查询,并且假定外键关系是健全的,SQLAlchemy通常会为您建立正确的联接,因此如果要列出所有表,则应该足够了餐馆,要做:

session.query(Restaurants).all()

在这里,您将获得Restaurants个实例的列表,它们的.adresse属性通过您配置的关联表填充了关联的Adresse实例列表。它应使用secondary=的{​​{1}}参数提供的关联表来计算正确的联接关系。如果由于某种原因仍然无法正常工作,我们必须仔细观察一下,但总的来说就是这样。