drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23.79k stars 590 forks source link

[BUG]: MySQL generatedAlwaysAs with notNull #2616

Open rktyt opened 3 months ago

rktyt commented 3 months ago

What version of drizzle-orm are you using?

0.32.0

What version of drizzle-kit are you using?

0.23.0

Describe the Bug

import {
  mysqlTable,
  varchar,
  char,
} from 'drizzle-orm/mysql-core'
import { sql } from 'drizzle-orm'

export const test = mysqlTable('test', {
  nameKana: varchar('name_kana', { length: 50 }).notNull(),
  japaneseSyllabary: char('japanese_syllabary', { length: 1 })
    .generatedAlwaysAs(() => sql`SUBSTRING(name_kana, 1, 1)`, {
      mode: 'stored',
    })
    .notNull(),
})

The result of generating SQL using drizzle-kit generate from the above schema is as follows.

CREATE TABLE `test` (
    `name_kana` varchar(50) NOT NULL,
    `japanese_syllabary` char(1) NOT NULL GENERATED ALWAYS AS (SUBSTRING(name_kana, 1, 1)) STORED
);

This is a SQL syntax error. "NOT NULL" is in the wrong position.

Expected behavior

The correct SQL is the following

CREATE TABLE `test` (
    `name_kana` varchar(50) NOT NULL,
    `japanese_syllabary` char(1) GENERATED ALWAYS AS (SUBSTRING(name_kana, 1, 1)) STORED NOT NULL
);

Environment & setup

No response

AndriiSherman commented 3 months ago

fixing that, thanks!