SQLAlchemy Coaslesce与Select子查询

时间:2014-03-07 11:06:45

标签: sqlalchemy foreign-keys subquery coalesce

我正在尝试将以下SQL编写为SQLAlchemy查询:

SELECT COALESCE((
    SELECT client_id 
    FROM client_subclient_map m
    WHERE m.subclient_id = brands.client_id 
    LIMIT 1), 
    client_id)
FROM brands 
WHERE id = $1;

我目前有功能:

def client_subclient_map(self):
    return self.session.query(ClientSubclientMap).\
            filter(ClientSubclientMap.subclient_id==Brand.client_id).\
            limit(1).\
            subquery()

创建以下子查询:

SELECT client_subclient_map.client_id, client_subclient_map.subclient_id 
FROM client_subclient_map, brands 
WHERE client_subclient_map.subclient_id = brands.client_id
LIMIT :param_1

和主要功能:

def top_client(self, brand_id):
    clientmap_alias = aliased(ClientSubclientMap, self.client_subclient_map())
    self.query = self.session.query(
                    func.coalesce(
                        clientmap_alias.client_id, Brand.client_id
                    )).\
                    filter(Brand.id==brand_id)
    print self.query
    return self

创建查询:

SELECT coalesce(:param_1, brands.client_id) AS coalesce_1 
FROM brands 
WHERE brands.id = :id_1

然后我打电话

def get(self):
    return self.query.first()

组合时我的函数创建的完整查询如下所示:

SELECT coalesce(anon_1.client_id, brands.client_id) AS coalesce_1 
FROM (
    SELECT client_subclient_map.client_id AS client_id, 
        client_subclient_map.subclient_id AS subclient_id 
    FROM client_subclient_map, brands 
    WHERE client_subclient_map.subclient_id = brands.client_id
    LIMIT :param_1) AS anon_1, 
    brands 
WHERE brands.id = :id_1

这是错误的,因为select子查询发生在错误的地方,它需要在coalesce函数内部发生,而不是在FROM子句中发生。

我是SQLAlchemy的新手,所以它也可能是我设置中其他地方的问题。我在ClientSubclientMap表上的client_id和subclient_id列上都有ForeignKey引用,但是引用同一列Client.id的两个外键都存在一些问题,所以我删除了ClientSubclientMap.client外键引用。

sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition 
between parent/child tables on relationship ClientSubclientMap.subclients - 
there are multiple foreign key paths linking the tables.  Specify the 
'foreign_keys' argument, providing a list of those columns which should be 
counted as containing a foreign key reference to the parent table.

1 个答案:

答案 0 :(得分:0)

完成必须将JOIN重写为CASE语句作为解决方法。

def top_client(self, brand_id):
    self.query = self.session.query(case([(ClientSubclientMap.client_id==None, 
                                         Brand.client_id)],  
                                         else_=ClientSubclientMap.client_id))\
                    .outerjoin(ClientSubclientMap, 
                               Brand.client_id==ClientSubclientMap.subclient_id)\
                    .filter(Brand.id==brand_id)
    return self

构造查询:

SELECT 
    CASE 
        WHEN (client_subclient_map.client_id IS NULL) 
        THEN brands.client_id 
        ELSE client_subclient_map.client_id 
    END AS anon_1 
FROM brands 
    LEFT OUTER JOIN client_subclient_map 
        ON brands.client_id = client_subclient_map.subclient_id 
WHERE brands.id = :id_1

我仍然有兴趣知道如何在SELECT函数中执行嵌套COALESCE语句,但是如果有人可以提供帮助的话。