Почему я не могу использовать аргумент plsql в этом предложении where?

У меня есть функция ниже (is_organizer), которая работает и позволяет мне использовать этот метод в качестве вычисляемого поля в Hasura. Функция ниже (is_chapter_member), которая почти идентична, не работает.

РАБОТАЕТ

CREATE OR REPLACE FUNCTION is_organizer(event_row events, hasura_session json)
RETURNS boolean AS $$
  SELECT EXISTS (
    SELECT 1
    FROM event_organizers o
    WHERE
      o.user_id::text = hasura_session->>'x-hasura-user-id'
      AND
      (event_row.id = o.event_id OR event_row.event_template_id = o.event_template_id)
  );
$$ LANGUAGE SQL STRICT IMMUTABLE;

СЛОМАННЫЙ

CREATE OR REPLACE FUNCTION is_chapter_member(c chapters, hasura_session json)
RETURNS boolean AS $$
  SELECT EXISTS (
    SELECT 1
    FROM chapter_members m
    WHERE
      m.user_id::text = hasura_session->>'x-hasura-user-id'
      AND
      c.chapter_id = m.chapter_id
  );
$$ LANGUAGE SQL STRICT IMMUTABLE;

При попытке добавить эту функцию (не вызывать ее, а просто создать) Postgres выдает следующую ошибку:

ERROR:  missing FROM-clause entry for table "c"
LINE 9:       c.chapter_id = m.chapter_id

Зачем параметру функции нужно предложение where? Таблицы дампов ниже ...

                                         Table "public.chapters"
     Column      |           Type           | Collation | Nullable |               Default                
-----------------+--------------------------+-----------+----------+--------------------------------------
 id              | integer                  |           | not null | nextval('chapters_id_seq'::regclass)
 title           | text                     |           | not null | 
 slug            | text                     |           | not null | 
 description     | jsonb                    |           |          | 
 avatar_url      | text                     |           |          | 
 photo_url       | text                     |           |          | 
 region          | text                     |           |          | 
 maps_api_result | jsonb                    |           |          | 
 lat             | numeric(11,8)            |           |          | 
 lng             | numeric(11,8)            |           |          | 
 created_at      | timestamp with time zone |           | not null | now()
 updated_at      | timestamp with time zone |           | not null | now()
 deleted_at      | timestamp with time zone |           |          | 

                     Table "public.chapter_members"
   Column   |           Type           | Collation | Nullable | Default 
------------+--------------------------+-----------+----------+---------
 user_id    | integer                  |           | not null | 
 chapter_id | integer                  |           | not null | 
 created_at | timestamp with time zone |           | not null | now()
 updated_at | timestamp with time zone |           | not null | now()


                                                   Table "public.events"
      Column       |            Type             | Collation | Nullable |                      Default                      
-------------------+-----------------------------+-----------+----------+---------------------------------------------------
 id                | integer                     |           | not null | nextval('events_id_seq'::regclass)
 event_template_id | integer                     |           | not null | 
 venue_id          | integer                     |           |          | 
 starts_at         | timestamp without time zone |           | not null | 
 duration          | interval                    |           | not null | 
 title             | text                        |           |          | 
 slug              | text                        |           |          | 
 description       | text                        |           |          | 
 photo_url         | text                        |           |          | 
 created_at        | timestamp without time zone |           | not null | now()
 updated_at        | timestamp without time zone |           | not null | now()
 deleted_at        | timestamp without time zone |           |          | 
 ends_at           | timestamp without time zone |           |          | generated always as (starts_at + duration) stored


                                  Table "public.event_organizers"
      Column       |  Type   | Collation | Nullable |                   Default                    
-------------------+---------+-----------+----------+----------------------------------------------
 id                | integer |           | not null | nextval('event_organizers_id_seq'::regclass)
 user_id           | integer |           | not null | 
 event_id          | integer |           |          | 
 event_template_id | integer |           |          | 


person Trey Stout    schedule 20.10.2020    source источник
comment
попробуйте (c).chapters - в парсере могут быть проблемы, поскольку ‹x›. ‹y› определен как ссылка ‹table›. ‹column› стандартами SQL. Типы записей / строк поддерживаются не всеми СУБД.   -  person Richard Huxton    schedule 20.10.2020
comment
@RichardHuxton, ты был на правильном пути. Использование (c).chapter_id подняло следующую проблему, которая заключалась в отсутствии поля, окончательный ответ ниже.   -  person Trey Stout    schedule 20.10.2020


Ответы (1)


Оказалось, что это неправильное имя столбца в неработающей функции. chapter_id должен был быть просто id в c аргументе. Я воспользовался подсказкой Ричарда и попытался поставить скобки вокруг аргумента, например (c).chapter_id. Затем это правильно сказало мне, что chapter_id не существует, и позволило мне исправить проблему.

person Trey Stout    schedule 20.10.2020