PostgreSQL "Column does not exist" but it actually does - sql

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?

Try to take it into double quotes - like "Continent" in the query:
SELECT "Continent"
FROM network.countries
...

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' "))

This problem occurs in postgres 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".
See this:

Such an error can appear when you add a space in the name of a column by mistake (for example "users ").

QUICK FIX (TRICK)
If you have recently added a field which you have already deleted before and now trying to add the same field back then let me share you this simple trick! i did this and the problem was gone!!
so, now just delete the migration folder entirely on the app,then instead of adding that field you need to now add a field but with the name of which you have never declared on this app before, example if you are trying to add title field then create it by the name of heading and now do the migration process separately on the app and runserver, now go to admin page and look for that model and delete all the objects and come to models back and rename the field that you recently made and name it to which you were wishing it with earlier and do the migrations again and now your problem must have been gone!!
this occurs when the objects are there in the db but you added a field which wasn't there when the earlier objs were made, so by this we can delete those objs and make fresh ones again!

I got the same error when I do PIVOT in RedShift.
My code is similar to
SELECT *
INTO output_table
FROM (
SELECT name, year_month, sales
FROM input_table
)
PIVOT
(
SUM(sales)
FOR year_month IN ('nov_2020', 'dec_2020', 'jan_2021', 'feb_2021', 'mar_2021', 'apr_2021', 'may_2021', 'jun_2021', 'jul_2021', 'aug_2021',
'sep_2021', 'oct_2021', 'nov_2021', 'dec_2021', 'jan_2022', 'feb_2022', 'mar_2022', 'apr_2022', 'may_2022', 'jun_2022',
'jul_2022', 'aug_2022', 'sep_2022', 'oct_2022', 'nov_2022')
)
I tried year_month without any quote (got the error), year_month with double quote (got the error), and finally year_month with single quote (it works this time). This may help if someone in the same situation like my example.

Related

Azure Data Studio not respect specified casing with PostgreSQL [duplicate]

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?
Try to take it into double quotes - like "Continent" in the query:
SELECT "Continent"
FROM network.countries
...
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' "))
This problem occurs in postgres 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".
See this:
Such an error can appear when you add a space in the name of a column by mistake (for example "users ").
QUICK FIX (TRICK)
If you have recently added a field which you have already deleted before and now trying to add the same field back then let me share you this simple trick! i did this and the problem was gone!!
so, now just delete the migration folder entirely on the app,then instead of adding that field you need to now add a field but with the name of which you have never declared on this app before, example if you are trying to add title field then create it by the name of heading and now do the migration process separately on the app and runserver, now go to admin page and look for that model and delete all the objects and come to models back and rename the field that you recently made and name it to which you were wishing it with earlier and do the migrations again and now your problem must have been gone!!
this occurs when the objects are there in the db but you added a field which wasn't there when the earlier objs were made, so by this we can delete those objs and make fresh ones again!
I got the same error when I do PIVOT in RedShift.
My code is similar to
SELECT *
INTO output_table
FROM (
SELECT name, year_month, sales
FROM input_table
)
PIVOT
(
SUM(sales)
FOR year_month IN ('nov_2020', 'dec_2020', 'jan_2021', 'feb_2021', 'mar_2021', 'apr_2021', 'may_2021', 'jun_2021', 'jul_2021', 'aug_2021',
'sep_2021', 'oct_2021', 'nov_2021', 'dec_2021', 'jan_2022', 'feb_2022', 'mar_2022', 'apr_2022', 'may_2022', 'jun_2022',
'jul_2022', 'aug_2022', 'sep_2022', 'oct_2022', 'nov_2022')
)
I tried year_month without any quote (got the error), year_month with double quote (got the error), and finally year_month with single quote (it works this time). This may help if someone in the same situation like my example.

Problems using cubeSQL trying to add data to table

I just got cubeSQL admin and SQL Lite manager, and am new at this, trying to create a database for an mobile app to get video info and urls from to stream. I set up a database and connected it to the manager, but cannot get it to accept the script that I am using. This is what i am putting in to get it to add data to a table.
INSERT INTO Sabbath School
VALUES
(number 1, hello, great, google.com, google.com),
This is the error I get:
Here are screenshots of what I am working with. The first one is the database:
The next one is the table configuration
The final one is what the table looks like.
Any help would be most appreciated as to what I am doing wrong here. I am really not knowing what I am doing and trying to learn how to use sql.
This looks a "quoted identifier" issue. Since the table name has a space in it, you will need to surround the table in double quotes. The query parser believes your table name is "Sabbath" and is expecting the VALUES keyword next, or an opening parenthesis ( to start your (column list). Since it sees "School" next, you get the syntax error. My preference is to avoid spaces in table names so you don't need to quote it all the time.

Azure Monitor Logs: The AppServiceHTTPLogs table is missing the CsUriQuery field?

I'm trying to get some raw log-data from the AppServiceHTTPLogs table by using Azure > Monitor > Logs.
As it turns out, the AppServiceHTTPLogs table doesn't seem to have the CsUriQuery column, which is weird.
For example, the query:
AppServiceHTTPLogs | project CsUriStem
.. returns requested file names as expected, but the query:
AppServiceHTTPLogs| project CsUriQuery
.. returns error (Failed to resolve scalar expression named 'CsUriQuery')
Why's that? Is the CsUriQuery column totally missing at the AppServiceHTTPLogs table?
Also, a weird thing is that another table, W3CIISLog, has the column in place (csUriQuery) but that table, when queried with the Monitor, returns no rows at all (missing data)?!
Any comments? Thanks.

Synonyms have stopped working in BigQuery

All of a sudden I cannot get synonyms for tables working in BigQuery, so a query like the following works fine:
select id as id, value as value
from pos_dw_api.test
But a query like the following fails:
select a.id as id, a.value as value
from pos_dw_api.test a
The error returned is the following. I have run this from the web console:
Query Failed
Error: Unknown field: a.id
Synonyms were working just fine last week ... The example table I'm using for this select is 387047224813.pos_dw_api.test.
Has the syntax for synonyms changed? Is this a bug?
Table synonyms generally only work when you're doing a JOIN. I don't know of anything that would have caused this to change. I realize that this is kind of strange, and I've filed an internal bug to fix it.

Unable To Run Access Report With Column Name Which Has Special Character

I'm trying to run a report in Access that references a poorly named column: Vendor#. For those of you not familiar with Access: '#' is a reserved keyword with special meaning. I've been trying to run the report and every time I do a popup appears asking for a value for the column: in other words it keeps seeing it as a variable name. I've tried a number of variations on the name including: [Vendor#], 'Vendor#', ['Vendor#']. I tried an Alias but then I encountered the same issue in the where clause referencing the Alias. No I can't change the schema to rename the column to something more appropriate. Any help is appreciated.
Here is the query:
SELECT * FROM dbo_Vendors
WHERE ((dbo_Vendors.[Vendor#]) = [Forms]![frm_Report_Vendor]![VendorNumber])
I have just tested with a linked sql server table having a column called Vendor#. I can create a report and it runs correctly.
Can you save the query and test that the query runs? I suspect that you may have a misspelled form reference, because I have tested the whole scenario and it works for me. I do not think the report has anything to do with it.