开发者

phantom "name" column?

I start simple:

hoops=# select * from core_school limit 3;
 id |   school_name    | nickname 
----+------------------+----------
  1 | Marshall         | 
  2 | Ohio             | 
  3 | Houston          | 
(10 rows)

Let's introduce an intentional error:

hoops=# select name from core_school;
ERROR:  co开发者_运维百科lumn "name" does not exist
LINE 1: select name from core_school;

But why does this work? (with an unexpected result!):

hoops=# select core_school.name from core_school limit 3;
  name       
-----------------
(1,Marshall,"")
(2,Ohio,"")
(3,Houston,"")
(3 rows)

Where did the "name" column come from in the third query?


This is PostgreSQL's autocast feature which allows calling function(argument) as argument.function.

What you are really calling is

SELECT  NAME(core_school)
FROM    core_school

Compare to this:

SELECT  (1::int).exp
--
2.71828182845905

which is quite self-explaining.

This "feature" very often leads to confusion and will (finally) be removed in 9.1.


Maybe you have a different version of Postgres than I do. (I've got 8.3.7.) But I don't have any such "phantom" name column.

If you simply say "select core_school from core_school" you'll get one line of output for each row in the table, with that line consisting of an array of the values of all the columns in the table. That's what you're seeing.

Oh, I notice that you're getting a column name of dealer. Maybe you didn't real put a period between "core_school" and "name" but a space, and now "name" is an alias for the column name. (My Postgres installation requires the word "as" to make an alias for a column name, but some databases do not require this, so maybe there's an option in Postgres somewhere for compatibility.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜