© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•9mo ago•
6 replies
Father Christmas

Non hardcoded strings in sql()

This:

import { sql } from 'drizzle-orm';
import { check, int, sqliteTable, text } from 'drizzle-orm/sqlite-core';


const SOME_KIND_ENUM  = ["win", "sbst", "tbst", "chop"] as const;

export const someTable = sqliteTable("some_table", {
  id: int().primaryKey({ autoIncrement: true }),
  kind: text({ enum: SOME_KIND_ENUM }).notNull(),
}, (table) => [
  check(
            "is_valid",
      sql`${table.kind} IN ('${SOME_KIND_ENUM.join(`', '`)}')`
        ),
]);
import { sql } from 'drizzle-orm';
import { check, int, sqliteTable, text } from 'drizzle-orm/sqlite-core';


const SOME_KIND_ENUM  = ["win", "sbst", "tbst", "chop"] as const;

export const someTable = sqliteTable("some_table", {
  id: int().primaryKey({ autoIncrement: true }),
  kind: text({ enum: SOME_KIND_ENUM }).notNull(),
}, (table) => [
  check(
            "is_valid",
      sql`${table.kind} IN ('${SOME_KIND_ENUM.join(`', '`)}')`
        ),
]);


produces:

CREATE TABLE `some_table` (
    `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
    `kind` text NOT NULL,
    CONSTRAINT "is_valid" CHECK("some_table"."kind" IN ('?'))
);
CREATE TABLE `some_table` (
    `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
    `kind` text NOT NULL,
    CONSTRAINT "is_valid" CHECK("some_table"."kind" IN ('?'))
);


I would really prefer to have one source of truth for this enum. This way I have one place from where I can drive the type and all the other stuff.
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

non-SQL code in RQB extras
Drizzle TeamDTDrizzle Team / help
5mo ago
sql operator with array of strings
Drizzle TeamDTDrizzle Team / help
2y ago
How to use sql template strings
Drizzle TeamDTDrizzle Team / help
3y ago
Replacing hardcoded text value in `jsonb`
Drizzle TeamDTDrizzle Team / help
2y ago