Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask RESTful-查询SQLalchemy返回两个列表_Python_Sqlalchemy - Fatal编程技术网

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

Python Flask RESTful-查询SQLalchemy返回两个列表,python,sqlalchemy,Python,Sqlalchemy,我是python的初学者,目前正在开发一个api 问题是,当我连接两个表restaurants和address时,查询SQLAlchemy会向我发送两个列表,但我希望合并他的列表 我了解到: 我的两个班:餐厅和地址 restaurant_adresse_association = db.Table( 'restaurant_adresse', db.Column('restaurant_id', db.Integer, ForeignKey('restaurants.id')),

我是python的初学者,目前正在开发一个api

问题是,当我连接两个表restaurants和address时,查询SQLAlchemy会向我发送两个列表,但我希望合并他的列表

我了解到:

我的两个班:餐厅和地址

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
    }
]
测试:

结果:

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'.
结果:

[
    {
        "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
    }
]

您正在使用以下命令查询两个表:

session.query(Restaurants, Adresse)
这基本上相当于SQL语句,如

SELECT * FROM restaurants, adresse;
这会在表之间创建一个新的值,这可能不是您想要的。如果看不到champs_restaurant,很难说接下来会发生什么,但它似乎在尝试将包含两个表中的列的结果填充到用于组合结果的JSON格式中

在SQLAlchemy ORMs中配置关系的一部分要点是,您可以查询表,SQLAlchemy通常会为您构建正确的联接(假设外键关系正常),因此,如果您想列出所有餐厅,则只需执行以下操作即可:

session.query(Restaurants).all()

在这里,您将通过配置的关联表获得一个带有
属性的
餐厅
实例列表。ADRESE
属性填充了一个关联(
ADRESE
实例)列表。它应该使用
secondary=
参数为
relation
提供的关联表来计算正确的连接关系。如果出于某种原因,这仍然不起作用,我们将不得不仔细观察,但通常情况下,这是一个想法。

它们是列表而不是元组。我想感谢你回答我,我仍在学习,你的所有建议都很好。不,我肯定在我的关系中有错误,我已经用你的建议更新了我的问题。你认为“封送”可能是个问题吗?我更新了我的答案:事实上,你根本不需要显式的
.join(…)
。只要
session.query(餐厅)
就足够了。出现错误的原因是
Query.join(…)
过于简单,因为它不会自动考虑多对多关系,而是在一个表上查找一列或另一个表中包含另一个表的外键(在本例中不存在,因为关系通过关联表)。
SELECT * FROM restaurants, adresse;
session.query(Restaurants).all()