psycopg2.errors.undefinedtable:不存在关系(加入表)

发布于 2025-01-18 19:47:12 字数 620 浏览 3 评论 0原文

我正在尝试使用 postgreSQL 和 psycopg2 复制这个答案: https://stackoverflow.com/a/33632820/1200914

我的 SQL 代码看起来像(我只是将新行放在这里以获得更好的可读性):

UPDATE t SET mycolumn = a.mycolumn FROM mytable AS t INNER JOIN (VALUES (28625, '1'),
                        (56614, '1'),  (86517, '1') ) AS a(id, mycolumn) ON a.id = t.id

但是,我收到下一个错误:

psycopg2.errors.UndefinedTable: relation "t" does not exist

使用光标执行此 sql 时。在 mytable 中,我有一列名称为 mycolumn,另一列名称为 id,这是主键。我缺少什么?对了,情侣的顺序应该是这样的吧?只是问一下,因为在之前的答案中我认为用户交换了 id 和 value 值。

I'm trying to replicate this answer using postgreSQL and psycopg2: https://stackoverflow.com/a/33632820/1200914

My SQL code looks like (I just put the new line here for better readibility):

UPDATE t SET mycolumn = a.mycolumn FROM mytable AS t INNER JOIN (VALUES (28625, '1'),
                        (56614, '1'),  (86517, '1') ) AS a(id, mycolumn) ON a.id = t.id

However, I'm getting the next error:

psycopg2.errors.UndefinedTable: relation "t" does not exist

when executing this sql with my cursor. In mytable I have a column with the name mycolumn and another one with name id, which is the primary key. What am I missing? By the way, the order of the couples should be like this, no? Just asking because in the previous answer I think the user exchanged the id and value values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风流物 2025-01-25 19:47:12

为了让它工作,我将查询重新设计为:

更新。添加了 WHERE 子句。

UPDATE
    mytable
SET
    mycolumn = a.mycolumn::boolean
FROM
    mytable AS t
    INNER JOIN (
        VALUES (28625, '1'),
            (56614, '1'),
            (86517, '1')) AS a (id, mycolumn) ON a.id = t.id
    WHERE
        a.id = mytable.id
;

当我尝试您的原始查询时,我得到:

错误:多次指定表名“t”

当我尝试我的评论建议时,我得到:

错误:列引用“id”不明确 code>

这里的文档 UPDATE 有点令人困惑:

别名

目标表的替代名称。当提供别名时,它完全隐藏表的实际名称。例如,给定 UPDATE foo AS f,UPDATE 语句的其余部分必须将此表引用为 f 而不是 foo。

来自_项目

允许其他表中的列出现在 WHERE 条件和更新表达式中的表表达式。它使用与 SELECT 语句的 FROM 子句相同的语法;例如,可以指定表名的别名。不要将目标表重复作为 from_item,除非您打算进行自联接(在这种情况下,它必须在 from_item 中与别名一起出现)。

但鉴于错误消息,我认为 UPDATE 部分需要实际的表名称,而 FROM 部分需要别名。

To get it to work I reworked the query as:

UPDATED. Added WHERE clause.

UPDATE
    mytable
SET
    mycolumn = a.mycolumn::boolean
FROM
    mytable AS t
    INNER JOIN (
        VALUES (28625, '1'),
            (56614, '1'),
            (86517, '1')) AS a (id, mycolumn) ON a.id = t.id
    WHERE
        a.id = mytable.id
;

When I tried your original query I got:

ERROR: table name "t" specified more than once

When I tried my comment suggestion I got:

ERROR: column reference "id" is ambiguous

The docs from here UPDATE are somewhat confusing:

alias

A substitute name for the target table. When an alias is provided, it completely hides the actual name of the table. For example, given UPDATE foo AS f, the remainder of the UPDATE statement must refer to this table as f not foo.

from_item

A table expression allowing columns from other tables to appear in the WHERE condition and update expressions. This uses the same syntax as the FROM clause of a SELECT statement; for example, an alias for the table name can be specified. Do not repeat the target table as a from_item unless you intend a self-join (in which case it must appear with an alias in the from_item).

But given the error messages I figured the UPDATE portion needed the actual table name and the FROM needed the aliased name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文