一般的な関係演算子

この章では、関係に基づいて構築される演算子について説明します。

__eq __()

上記の演算子は、多対1の「等しい」比較です。この演算子のコード行は次のとおりです-

s = session.query(Customer).filter(Invoice.invno.__eq__(12))

上記のコード行の同等のSQLクエリは-です。

SELECT customers.id 
AS customers_id, customers.name 
AS customers_name, customers.address 
AS customers_address, customers.email 
AS customers_email
FROM customers, invoices
WHERE invoices.invno = ?

__ne __()

この演算子は、多対1の「等しくない」比較です。この演算子のコード行は次のとおりです-

s = session.query(Customer).filter(Invoice.custid.__ne__(2))

上記のコード行の同等のSQLクエリを以下に示します-

SELECT customers.id 
AS customers_id, customers.name 
AS customers_name, customers.address 
AS customers_address, customers.email 
AS customers_email
FROM customers, invoices
WHERE invoices.custid != ?

contains()

この演算子は1対多のコレクションに使用され、contains()−のコードを以下に示します。

s = session.query(Invoice).filter(Invoice.invno.contains([3,4,5]))

上記のコード行の同等のSQLクエリは-です。

SELECT invoices.id 
AS invoices_id, invoices.custid 
AS invoices_custid, invoices.invno 
AS invoices_invno, invoices.amount 
AS invoices_amount
FROM invoices
WHERE (invoices.invno LIKE '%' + ? || '%')

どれか()

以下に示すように、any()演算子がコレクションに使用されます-

s = session.query(Customer).filter(Customer.invoices.any(Invoice.invno==11))

上記のコード行の同等のSQLクエリを以下に示します-

SELECT customers.id 
AS customers_id, customers.name 
AS customers_name, customers.address 
AS customers_address, customers.email 
AS customers_email
FROM customers
WHERE EXISTS (
   SELECT 1
   FROM invoices
   WHERE customers.id = invoices.custid 
   AND invoices.invno = ?)

has()

この演算子は、次のようにスカラー参照に使用されます。

s = session.query(Invoice).filter(Invoice.customer.has(name = 'Arjun Pandit'))

上記のコード行の同等のSQLクエリは-です。

SELECT invoices.id 
AS invoices_id, invoices.custid 
AS invoices_custid, invoices.invno 
AS invoices_invno, invoices.amount 
AS invoices_amount
FROM invoices
WHERE EXISTS (
   SELECT 1
   FROM customers
   WHERE customers.id = invoices.custid 
   AND customers.name = ?)