SQL PostgreSQL“列不存在”但实际上确实存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41591386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:05:02  来源:igfitidea点击:

PostgreSQL "Column does not exist" but it actually does

sqlpostgresqlquoted-identifier

提问by BullyWiiPlaza

I'm writing a Javaapplication to automatically build and run SQL queries. For many tables my code works fine but on a certain table it gets stuck by throwing the following exception:

我正在编写一个Java应用程序来自动构建和运行 SQL 查询。对于许多表,我的代码工作正常,但在某个表上,它会因抛出以下异常而卡住:

Exception in thread "main" org.postgresql.util.PSQLException: ERROR: column "continent" does not exist
  Hint: Perhaps you meant to reference the column "countries.Continent".
  Position: 8

The query that has been run is the following:

已运行的查询如下:

SELECT Continent
FROM network.countries
WHERE Continent IS NOT NULL
AND Continent <> ''
LIMIT 5

This essentially returns 5non-empty values from the column.

这实际上5从列中返回非空值。

I don't understand why I'm getting the "column does not exist" error when it clearly does in pgAdmin 4. I can see that there is a schema with the name Networkwhich contains the table countriesand that table has a column called Continentjust as expected.

我不明白为什么我会在pgAdmin 4 中清楚地收到“列不存在”错误。我可以看到有一个名称Network包含该表的模式,countries并且该表有一个Continent按预期调用的列。

Since all column, schema and table names are retrieved by the application itself I don't think there has been a spelling or semantical error so why does PostgreSQLcause problems regardless? Running the query in pgAdmin4 nor using the suggested countries.Continentis working.

由于所有列、模式和表名都是由应用程序本身检索的,我认为没有拼写或语义错误,那么为什么PostgreSQL无论如何都会导致问题?在 pgAdmin4 中运行查询也不使用建议countries.Continent的工作。

My PostgreSQL version is the newest as of now:

我的 PostgreSQL 版本是目前最新的:

$ psql --version
psql (PostgreSQL) 9.6.1

How can I successfully run the query?

如何成功运行查询?

回答by Eugene Lisitsky

Try to take it into double quotes - like "Continent"in the query:

尝试将其放入双引号中 - 就像"Continent"在查询中一样:

SELECT "Continent"
FROM network.countries
...

回答by ISONecroMAn

In working with SQLAlchemy environment, i have got this error with the SQL like this,

在使用 SQLAlchemy 环境时,我遇到了这样的 SQL 错误,

   db.session.execute(
    text('SELECT name,type,ST_Area(geom) FROM buildings WHERE type == "plaza" '))

ERROR: column "plaza" does not exist

错误:“广场”列不存在

Well, i changed == by = , Error still persists, then i interchanged the quotes, like follows. It worked. Weird!

好吧,我将 == 改为 = ,错误仍然存​​在,然后我交换了引号,如下所示。有效。奇怪的!

.... 
text("SELECT name,type,ST_Area(geom) FROM buildings WHERE type = 'plaza' "))

回答by Kaori

This problem occurs because in pgAdmin3 because the table name is not tablename instead it is "tablename". for eg. If it shows user as table name, than table name is "user".

出现此问题的原因是在 pgAdmin3 中,因为表名不是表名而是“表名”。例如。如果它将用户显示为表名,则表名是“用户”。

See this:

看到这个:

image for table user

表用户的图像