[supabase gen] complex Views with triggers missing Insert/Update definitions

Open
#850 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
postgresql, typescript
Domain
databases, tooling

Research direction

Start by reproducing the schema and trigger from the issue, then run supabase gen types --lang typescript and inspect the generated public.Views.Profile definition. Trace the type-generation entry point for view metadata and add coverage for trigger-backed views. Done means the generated Profile type includes the expected Insert and Update sections without regressing automatically updatable views.

Written by the indexing model from the issue text.

Description

bug

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

supabase gen types does not properly handle the case that a complex view uses triggers to write information back through the view. It does not produce the Insert/ Update sections of the type definitions. When using simple "automatically updatable view this works as expected.

To Reproduce

We have two tables:

CREATE TABLE IF NOT EXISTS private.profile_type (
    id int2 NOT NULL PRIMARY KEY,
    name text NOT NULL,
);
CREATE TABLE IF NOT EXISTS private.profile (
  id uuid REFERENCES auth.users NOT NULL primary key,
  username text unique,
  profile_type_id int2 REFERENCES private.profile_type(id),
);

This view provides access to the profile and integrates information from the other table:

CREATE OR REPLACE VIEW public."Profile" ("id", "username", "profileType")
WITH (security_invoker) 
AS SELECT p.id, username, pt.name
FROM private.profile AS p
JOIN private.profile_type AS pt ON p.profile_type_id = pt.id;

This view is not an "automatically updatable view". In order to write back to the profile table, we are attaching a trigger to the view that handles the INSERT and UPDATE statements:

CREATE OR REPLACE FUNCTION public.profile_view_v1_row()
RETURNS TRIGGER
AS $$
BEGIN

  IF TG_OP = 'INSERT' THEN
    -- omitted code
  ELSE -- 'UPDATE'
    -- omitted code
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

CREATE OR REPLACE TRIGGER profile_view_v1_row
  INSTEAD OF INSERT OR UPDATE ON public."Profile"
  FOR EACH ROW
  EXECUTE FUNCTION public.profile_view_v1_row();

When generating the types using

supabase gen types --lang typescript

it does not include the sections for Insert and Update in the public->Views->Profile part of the generated types (see below)

Expected behavior

The expected type definition should look like this:

...
  public: {
    Tables: {
      [_ in never]: never
    }
    Views: {
      Profile: {
        Row: {
          id: string | null
          profileType: string | null
          username: string | null
        }

--> These parts are missing and should be generated
        Insert: {
          id?: string | null
          profileType?: string | null
          username?: string | null
        }
        Update: {
          id?: string | null
          profileType?: string | null
          username?: string | null
        }
<--
...
}

System information

  • OS: macOS
  • supabase cli version (supabase --version): 1.223.10
Dominant language
TypeScript
Stars
1.2k
Forks
223
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from supabase/postgres-meta

All issues in supabase/postgres-meta

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.