Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
jens-f opened this issue Nov 27, 2024 · 0 comments
Open
2 tasks done
Labels
bug Something isn't working

Comments

@jens-f
Copy link

jens-f commented Nov 27, 2024

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
@jens-f jens-f added the bug Something isn't working label Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant