簡體   English   中英

SQLAlchemy將外部聯接ORM查詢轉換為Core

[英]SQLAlchemy convert outer join ORM query to Core

使用核心組件時,我遇到SQLAlchemy的 select_from語句問題。 我嘗試構建一個外連接查詢,目前看起來像:

 
 
 
 
  
  
  query = select([b1.c.id, b1.c.num, n1.c.name, n1.c.num, ...] ).where(and_( ... some conditions ... ) ).select_from( ???.outerjoin( n1, and_( ... some conditions ... ) ).select_from(... more outer joins similar to the above ...)
 
 
  

根據文檔,結構應如下所示:

 
 
 
 
  
  
  table1 = table('t1', column('a')) table2 = table('t2', column('b')) s = select([table1.ca]).\\ select_from( table1.join(table2, table1.ca==table2.cb) )
 
 
  

我的問題是在這種情況下我沒有table1對象,因為 select ...部分由列而不是單個表組成(請參閱我的查詢中的問號)。 我嘗試過使用 n1.outerjoin(n1... ,但是這會導致異常( Exception: (ProgrammingError) table name "n1" specified more than once )。

上面的代碼片段來自基於工作會話的(ORM)查詢,我試圖轉換(成功有限)。

ProgrammingError: (ProgrammingError) table name "customer1" specified more than once
 'SELECT customer1.id, customer.name, order1.id, order1.order_num, address1.id, address1.city \nFROM customer, customer AS customer1 LEFT OUTER JOIN "order" AS order1 ON order1.customer_id = customer1.id, customer AS customer1 LEFT OUTER JOIN address AS address1 ON customer1.id = address1.customer_id' {}

如何將此ORM查詢轉換為純Core查詢? 更新:

我能夠縮小問題的范圍。 似乎兩個select_from調用的組合導致了問題。

 customer = Table('customer', metadata, Column('id', Integer), Column('name', String(50)), ) order = Table('order', metadata, Column('id', Integer), Column('customer_id', Integer), Column('order_num', Integer), ) address = Table('address', metadata, Column('id', Integer), Column('customer_id', Integer), Column('city', String(50)), ) metadata.create_all(db) customer1 = aliased(customer, name='customer1') order1 = aliased(order, name='order1') address1 = aliased(address, name='address1') columns = [ customer1.c.id, customer.c.name, order1.c.id, order1.c.order_num, address1.c.id, address1.c.city ] query = select(columns) query = query.select_from( customer1.outerjoin( order1, and_( order1.c.customer_id==customer1.c.id, ) ) ) query = query.select_from( customer1.outerjoin( address1, and_( customer1.c.id==address1.c.customer_id ) ) ) result = connection.execute(query) for r in result.fetchall(): print r 

上面的代碼導致以下異常:

 ProgrammingError: (ProgrammingError) table name "customer1" specified more than once 'SELECT customer1.id, customer.name, order1.id, order1.order_num, address1.id, address1.city \\nFROM customer, customer AS customer1 LEFT OUTER JOIN "order" AS order1 ON order1.customer_id = customer1.id, customer AS customer1 LEFT OUTER JOIN address AS address1 ON customer1.id = address1.customer_id' {} 

如果我在使用SQLAlchemy方面有一點經驗,我會說這可能是一個錯誤......

我終於設法解決了這個問題。 而不是級聯select_from ,需要將其他連接鏈接到實際連接。 上面的查詢將是:

query = select(columns)
query = query.select_from(
    customer1.outerjoin(
        order1,
        and_(
            order1.c.customer_id==customer1.c.id,
        )
    ).outerjoin(
        address1,
        and_(
            customer1.c.id==address1.c.customer_id
        )
    )
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM