更新SQLAlchemy中的Join

时间:2018-11-20 23:13:14

标签: python sqlalchemy

我正在将psycopg2接口转换为SQLAlchemy,并且具有以下SQL UPDATE查询,该查询是SELECT JOIN的postgresql合并,但为UPDATE重新编写。表A需要更新。表B包含大多数数据,可让我们确定表A是否需要更新。

这如何在SQLAlchemy中以与数据库无关的方式实现?

UPDATE A 
  SET takenoaction = TRUE,
      usercomment = 'We do not handle this matter', 
      processeddate =  now() 
  FROM B 
  WHERE 
    B.Aid = A.id AND 
    A.processeddate IS NULL AND 
    B.correspondencetype = 'Not For Us' AND 
    B.processthis = FALSE;

这是表格的样子:

CREATE TABLE A (
    id SERIAL PRIMARY KEY,
    takenoaction boolean DEFAULT FALSE,
    usercomment text,
    processeddate timestamp,
    actiontaken text
);

CREATE TABLE B (
    id SERIAL PRIMARY KEY,
    Aid integer REFERENCES A(id) ON DELETE CASCADE ON UPDATE CASCADE,
    correspondencetype text,
    processthis boolean DEFAULT TRUE
);

谢谢。

0 个答案:

没有答案