Skip to content

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

Open
@jens-f

Description

@jens-f

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions