supabase/postgres-meta

Incorrect typescript types for nested embedding

timbovelander opened this issue · 1 comments

Describe the bug
The generated typescript types are incorrect when using nested embedding in a query. Gives the error:

`Property 'protertyName' does not exist on type 'SelectQueryError<"Referencing missing column 'propertyName'">[].`

To Reproduce
Steps to reproduce the behavior:

  1. Create 3 tables, one has a foreign keys to the each of the other 2 tables.
create table public.recipes (
  id bigint primary key generated always as identity,
  title text not null
);

create table public.ingredients (
  id bigint primary key generated always as identity,
  title text not null
);

create table public.recipe_ingredients (
  recipe bigint not null references recipes on update cascade on delete cascade,
  ingredient bigint not null references ingredients on update cascade,
  constraint recipe_ingredients_pkey primary key (one, two)
);
  1. Generate types using npx supabase gen types typescript --local > /some/path/types.ts
  2. Make a query using a client:
const recipe = await supabase
  .from("recipes")
  .select("title, recipe_ingredients(ingredient(title))")
  .eq("id", id)
  .maybeSingle();
  1. When trying to access the property, you get the error as mentioned above:
recipe.data.recipe_ingredients.map((recipe_ingredient) => recipe_ingredient.ingredient.title);

Expected behavior
I expected the type to be string.

Screenshots
image

System information
Rerun the failing command with --create-ticket flag.

  • Ticket ID: -
  • Version of OS: Ubuntu 22.04.4
  • Version of CLI: v1.163.6
  • Version of Docker: v26.1.0
  • Versions of services: [output from supabase services command]
       SERVICE IMAGE      │        LOCAL         │  LINKED
 ─────────────────────────┼──────────────────────┼────────────
   supabase/postgres      │ 15.1.1.41            │ 15.1.1.41
   supabase/gotrue        │ v2.148.0             │ v2.148.0
   postgrest/postgrest    │ v12.0.2              │ v12.0.2
   supabase/realtime      │ v2.28.32             │ -
   supabase/storage-api   │ v1.0.10              │ v1.0.10
   supabase/edge-runtime  │ v1.45.2              │ -
   supabase/studio        │ 20240422-5cf8f30     │ -
   supabase/postgres-meta │ v0.80.0              │ -
   supabase/logflare      │ 1.4.0                │ -
   bitnami/pgbouncer      │ 1.20.1-debian-11-r39 │ -
   darthsim/imgproxy      │ v3.8.0               │ -

Additional context
If applicable, add any other context about the problem here.

  • Version of @supabase/ssr v0.3.0
  • Version of Node.js v20.11.0

Hey there !

I couldn't reproduce the issue on my end. I've tried it with the following setup (had to change the sql as it wasn't valid):

create table public.recipes (
  id bigint primary key generated always as identity,
  title text not null
);

create table public.ingredients (
  id bigint primary key generated always as identity,
  title text not null
);

create table public.recipe_ingredients (
  recipe bigint not null references recipes on update cascade on delete cascade,
  ingredient bigint not null references ingredients on update cascade,
  constraint recipe_ingredients_pkey primary key (recipe, ingredient)
);

Which generated the following types after introspection:

  ingredients: {
    Row: {
      id: number
      title: string
    }
    Insert: {
      id?: never
      title: string
    }
    Update: {
      id?: never
      title?: string
    }
    Relationships: []
  }
  recipe_ingredients: {
    Row: {
      ingredient: number
      recipe: number
    }
    Insert: {
      ingredient: number
      recipe: number
    }
    Update: {
      ingredient?: number
      recipe?: number
    }
    Relationships: [
      {
        foreignKeyName: "recipe_ingredients_ingredient_fkey"
        columns: ["ingredient"]
        isOneToOne: false
        referencedRelation: "ingredients"
        referencedColumns: ["id"]
      },
      {
        foreignKeyName: "recipe_ingredients_recipe_fkey"
        columns: ["recipe"]
        isOneToOne: false
        referencedRelation: "recipes"
        referencedColumns: ["id"]
      },
    ]
  }
  recipes: {
    Row: {
      id: number
      title: string
    }
    Insert: {
      id?: never
      title: string
    }
    Update: {
      id?: never
      title?: string
    }
    Relationships: []
  }

Then reproducing the mentioned selection here I got expected type inference:

Image

Image

You can find the MRE I've used here: supabase/postgrest-js@master...avallete/mre-837-test
Let me know if this is still an issue for you @timbovelander with the latest versions of postgres-meta (in the MRE it's 0.87.1) and supabase-js/postgrest-js.