tags:

views:

593

answers:

3

I have SQL query, which is working nice on Oracle and MSSQL. Now I'm trying this on PostgreSQL and it gives a strange exception: org.postgresql.util.PSQLException: ERROR: missing FROM-clause entry for table "main"

Here is the query:

SELECT *
FROM "main" main
     INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
     INNER JOIN "something" somehting ON something_link."somethingid" = something."id"
     INNER JOIN "type" type ON something."typeid" = type."id"

This is quite simple query and I can't see why it is not working on Windows XP SP2, PostgreSQL 8.3?

+2  A: 

somehting=>something


postgres=# create database test
postgres-# ;
CREATE DATABASE

postgres=# \c test
You are now connected to database "test".

test=# select version();
                                            version                                            
-----------------------------------------------------------------------------------------------
 PostgreSQL 8.3.3 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)

test=# create table main(id int);
CREATE TABLE

test=# create table something_link(mainid int);
CREATE TABLE

test=# create table something(id int);
CREATE TABLE

test=# create table type(id int);
CREATE TABLE

test=# alter table something add column typeid int;
ALTER TABLE

test=# SELECT *
test-# FROM "main" main
test-#      INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
test-#      INNER JOIN "something" somehting ON something_link."somethingid" = something."id"
test-#      INNER JOIN "type" type ON something."typeid" = type."id"
test-# ;
ERROR:  column something_link.somethingid does not exist
LINE 4:      INNER JOIN "something" somehting ON something_link."som...
                                                 ^
test=# alter table something_link add column somethingid int;
ALTER TABLE

test=# SELECT *                                              
FROM "main" main
     INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
     INNER JOIN "something" *somehting* ON something_link."somethingid" = something."id"
     INNER JOIN "type" type ON something."typeid" = type."id"
;

ERROR:  invalid reference to FROM-clause entry for table "something"
LINE 4: ...hing" somehting ON something_link."somethingid" = something....
                                                             ^
HINT:  Perhaps you meant to reference the table alias "somehting".

test=# SELECT *
FROM "main" main
     INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
     INNER JOIN "something" something ON something_link."somethingid" = something."id"
     INNER JOIN "type" type ON something."typeid" = type."id"
;

 id | mainid | somethingid | id | typeid | id 

----+--------+-------------+----+--------+----

(0 rows)
l_39217_l
Thank you! There was really typing mistake! But this was not the first problem. Next time I will not post question late in the night, but wait until next morning with fresh mind :) I'll explain in answer as well the next problem.
m_pGladiator
+1  A: 

According to this, seems like you either mistyped an alias or used a table name in place of it.

Vinko Vrsalovic
Thank you! Actually, there was a spelling mistake but the real problem as you pointed me in the documentation was different. See my post below.
m_pGladiator
+1  A: 

The real problem is actually not the query, but the PostgreSQL 8.3 default configuration. After correcting the spelling mistake (10x Kendrick Wilson), the problem persisted, until I edited the "postgresql.conf" file. There should be a line:

add_missing_from = on

This line ensures compatibility with the other SQL dialects.

m_pGladiator
No, this adds the 'missing from' only, which may or may not be compatible with other dialects. Glad you solved your problem though.
Vinko Vrsalovic
I mean that even with add_missing_from = on, different compatibility issues still persist. In fact I'm not even sure if MSSQL and Oracle 'add missing froms' too... But you say so, so I'll believe you :)
Vinko Vrsalovic