Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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 栏「;栏「;必须出现在GROUPBY子句中--SQLAlchemy_Python_Postgresql_Sqlalchemy - Fatal编程技术网

Python 栏「;栏「;必须出现在GROUPBY子句中--SQLAlchemy

Python 栏「;栏「;必须出现在GROUPBY子句中--SQLAlchemy,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,请问,为什么我会被冠以“错误”的标题,尽管事实上,我的名字出现在组_中提到的列中。下面是我的问题 products = session .query(User) \ .join(User.products) \ .join(Product.productitems) \ .values( Product.title.label('title'),(func.count(Prod

请问,为什么我会被冠以“错误”的标题,尽管事实上,我的名字出现在组_中提到的列中。下面是我的问题

products = session
               .query(User) \
               .join(User.products) \
               .join(Product.productitems) \
               .values( Product.title.label('title'),(func.count(ProductItem.id)).label('total')) \
               .group_by(Product.id,Product.title) \
               .order_by(Product.created_date)
模型类

class User(UserMixin , Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    title = Column(CHAR(3), nullable = True)

class Product(Base):
    __tablename__ = 'products'
    id = Column(Integer, primary_key =True)
    title = Column(String(250), nullable = False)
    ..
    user_id = Column(Integer, ForeignKey('users.id'))
    user = relationship('User',backref=backref("products", cascade="all, delete-orphan"),lazy='joined')

class ProductItem(Base):
    __tablename__ ='product_items'
    id = Column(Integer , primary_key = True)
    ...
    product_id = Column(Integer, ForeignKey('products.id'))
    product = relationship('Product',backref=backref("productitems", cascade="all, delete-orphan"),lazy='joined' )
在控制台中,我可以看到查询返回

SELECT 
    products.title AS title
   ,COUNT(product_items.id) AS total 
FROM 
    users 
        JOIN products ON users.id = products.user_id 
        JOIN product_items ON products.id = product_items.product_id

然后在group_by上出现错误并中断。请问它要什么?任何帮助都将不胜感激。

SQLalchemy要求链中的
values
为最后一项,因为它不会返回查询以继续。你需要做的可能是(未经测试的)


我怀疑您还需要将
Product.created\u date
聚合或添加到group by,因为您是通过它进行订购的。您不需要
title
(也不需要
created\u date
)在
group by
中,因为它们在功能上依赖于
Product.id
(已经在
group by
子句中)。但是,您调试的查询(从控制台)根本不包含
GROUP BY
子句,这可能会产生此错误消息。很抱歉,这是我的错误。请不要理会我以前的评论。不管怎样,错误保持不变,我添加了products.title列,或者从组中删除了products.title列。它仍然要求将其添加到组中。谢谢,先生。错误消失了。请给我时间测试。
products = session.query(User)                                               \
                  .join(User.products)                                       \
                  .join(Product.productitems)                                \
                  .group_by(Product.id, Product.title, Product.created_date) \
                  .order_by(Product.created_date)                            \
                  .values( Product.id.label('id'),                           \
                           Product.title.label('title'),                     \      
                           (func.count(ProductItem.id)).label('total'))