psycopg2.errors.UndefinedTable:关系不存在(加入table)
psycopg2.errors.UndefinedTable: relation does not exist (join table)
我正在尝试使用 postgreSQL 和 psycopg2 复制这个答案:
我的 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 值。
为了使其正常工作,我将查询修改为:
已更新。添加了 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
;
当我尝试您的原始查询时,我得到:
ERROR: table name "t" specified more than once
当我尝试我的评论建议时,我得到:
ERROR: column reference "id" is ambiguous
这里 UPDATE 的文档有些混乱:
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).
但是根据错误消息,我认为 UPDATE
部分需要实际的 table 名称,而 FROM
需要别名。
我正在尝试使用 postgreSQL 和 psycopg2 复制这个答案:
我的 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 值。
为了使其正常工作,我将查询修改为:
已更新。添加了 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
;
当我尝试您的原始查询时,我得到:
ERROR: table name "t" specified more than once
当我尝试我的评论建议时,我得到:
ERROR: column reference "id" is ambiguous
这里 UPDATE 的文档有些混乱:
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).
但是根据错误消息,我认为 UPDATE
部分需要实际的 table 名称,而 FROM
需要别名。