错误:关系"作者"不存在SQL状态:42P01

时间:2016-12-07 15:49:54

标签: postgresql plpgsql pgadmin

我是Postgresql的初学者,我正在尝试用内部联接创建一个简单的函数

CREATE OR REPLACE FUNCTION GetBooks() 
RETURNS TABLE( Id int, Title text, AuthorName text) as
$BODY$   
BEGIN
SELECT bo.Id , bo.Title , au.Name
        FROM "Book" bo INNER JOIN Author au ON (au.Id = bo.AuthorId);

END;
$BODY$
  LANGUAGE plpgsql;

当我执行以下

SELECT public.getbooks()

我收到此错误

ERROR:  relation "author" does not exist
LINE 2:       FROM "Book" bo INNER JOIN Author au ON (au.Id = bo.Aut...
                                    ^
QUERY:  SELECT bo.Id , bo.Title , au.Name
        FROM "Book" bo INNER JOIN Author au ON (au.Id = bo.AuthorId)
CONTEXT:  PL/pgSQL function getbooks() line 3 at SQL statement
********** Error **********

ERROR: relation "author" does not exist
 SQL state: 42P01
Context: PL/pgSQL function getbooks() line 3 at SQL statement

Image where we can see relation exists

Full Schema

1 个答案:

答案 0 :(得分:1)

CREATE OR REPLACE FUNCTION GetBooks() 
RETURNS TABLE( Id int, Title text, AuthorName text) as
$BODY$   
BEGIN
  RETURN QUERY 
SELECT bo."Id" , bo."Title" , au."Name"
        FROM "Book" bo INNER JOIN "Author" au ON (au."Id" = bo."AuthorId");

END;
$BODY$
  LANGUAGE plpgsql;