Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL "Column does not exist" but it actually does

I'm writing a Java application 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:

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 5 non-empty values from the column.

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 Network which contains the table countries and that table has a column called Continent just as expected.

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 PostgreSQL cause problems regardless? Running the query in pgAdmin4 nor using the suggested countries.Continent is working.

My PostgreSQL version is the newest as of now:

$ psql --version psql (PostgreSQL) 9.6.1 

How can I successfully run the query?

like image 371
BullyWiiPlaza Avatar asked Jan 11 '17 12:01

BullyWiiPlaza


People also ask

Where column does not exist Postgres?

PostgreSQL column does not exist exception occurs when we have used column did not exist in the table or it will occur when the used column name has lower case name and we have used upper case in our query.

Does not exist in PostgreSQL?

The NOT EXISTS is opposite to EXISTS . It means that if the subquery returns no row, the NOT EXISTS returns true. If the subquery returns one or more rows, the NOT EXISTS returns false.

How do you add column if not exists PostgreSQL?

The Postgres IF NOT EXISTS syntaxFirst, we specify the name of the table to which we want to add a column. We supply the IF NOT EXISTS option after the ADD COLUMN clause, and then we specify the name of the column and its data type.


2 Answers

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

SELECT "Continent" FROM network.countries ... 
like image 140
Eugene Lisitsky Avatar answered Sep 20 '22 08:09

Eugene Lisitsky


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

   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' ")) 
like image 43
ISONecroMAn Avatar answered Sep 21 '22 08:09

ISONecroMAn