psycopg2.errors.undefinedtable:不存在关系(加入表)
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了让它工作,我将查询重新设计为:
更新。添加了
WHERE
子句。当我尝试您的原始查询时,我得到:
错误:多次指定表名“t”
当我尝试我的评论建议时,我得到:
错误:列引用“id”不明确
code>这里的文档 UPDATE 有点令人困惑:
但鉴于错误消息,我认为 UPDATE 部分需要实际的表名称,而 FROM 部分需要别名。
To get it to work I reworked the query as:
UPDATED. Added
WHERE
clause.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:
But given the error messages I figured the
UPDATE
portion needed the actual table name and theFROM
needed the aliased name.