kysely-org / kysely

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

Error with returning when insertInto #941

Closed IRediTOTO closed 6 months ago

IRediTOTO commented 6 months ago

I have a schema like this

Types schema ``` import type { ColumnType } from "kysely"; export type Generated = T extends ColumnType ? ColumnType : ColumnType; export type Timestamp = ColumnType; export type Category = { id: number; name: string; }; export type CategoryToPost = { A: number; B: number; }; export type Img = { id: number; url: string; }; export type Post = { id: number; content: string; excerpt: string; featureImgId: number; }; export type PostToTag = { A: number; B: number; }; export type Tag = { id: number; name: string; }; export type DB = { _CategoryToPost: CategoryToPost; _PostToTag: PostToTag; Category: Category; Img: Img; Post: Post; Tag: Tag; }; ```
Kysely define file ```ts fimport { DB } from "./types"; // this is the Database interface we defined earlier import { createPool, createConnection } from "mysql2"; // do not use 'mysql2/promises'! import { Kysely, MysqlDialect, ParseJSONResultsPlugin } from "kysely"; const dialect = new MysqlDialect({ pool: createPool({ uri: process.env.DATABASE_URL }), }); // Database interface is passed to Kysely's constructor, and from now on, Kysely // knows your database structure. // Dialect is passed to Kysely's constructor, and from now on, Kysely knows how // to communicate with your database. export const db = new Kysely({ dialect, }); ```

I am trying to create Img like this

   const createImg = await db
        .insertInto("Img")
        .values({ id: 3, url: "urdl" })
        .returning(["id"])
        .executeTakeFirstOrThrow();

but I get this error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'returning `id`' at line 1",
  sql: "insert into `Img` (`id`, `url`) values (3, 'urdl') returning `id`

What did I do wrong? I just start using Kysely.

igalklebanov commented 6 months ago

Hey 👋

MySQL doesn't support the returning clause.

Please go through Getting Started with MySQL.