kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.44k stars 266 forks source link

Relation does not exist error in Supabase #437

Closed 0xQilin closed 1 year ago

0xQilin commented 1 year ago

Hi all, am trying to get Kysely set up with supabase. I've settled for this configuring

export const db = new Kysely<DB>({
  // PostgresDialect requires the Cursor dependency
  dialect: new PostgresDialect({
    pool: new Pool({
      URL: process.env.DATABASE_URL as string,
      database: "postgres",
    }),
    cursor: Cursor,
  }),
  // MysqlDialect doesn't require any special configuration
});

And I'm trying to execute this specific command in an api route handler in NextJS

import { db } from "@/src/lib/db";

export async function GET(request: Request) {
  const query = await db.selectFrom("company").selectAll().execute();
  return new Response("Hello, Next.js!");
}

But i'm getting an error which states that

error - error: relation "company" does not exist
    at Parser.parseErrorMessage (webpack-internal:///(sc_server)/./node_modules/pg-protocol/dist/parser.js:287:98)
    at Parser.handlePacket (webpack-internal:///(sc_server)/./node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse (webpack-internal:///(sc_server)/./node_modules/pg-protocol/dist/

I've verified that this table does indeed exist by looking at the supabase portal and am not really sure what might be the issue here.

Screenshot 2023-04-21 at 11 31 59 PM

igalklebanov commented 1 year ago

Hey 👋

//PostgresDialect requires the Cursor dependency

That's wrong, it's an optional field in PostgresDialectConfig, and only required (via runtime exceptions) if you're using .stream() in various query builders.


  pool: new Pool({
      URL: process.env.DATABASE_URL as string,
      database: "postgres",
    }),

You should be passing connection strings to Pool as follows:

   pool: new Pool({
       connectionString: process.env.DATABASE_URL!,
   }),

docs

0xQilin commented 1 year ago

Thanks so much @igalklebanov! Really appreciate the help with this. Works like a charm now. The route handler is .... insanely fast.