From 6e58e17be104a0a4857d6138b5592a9a2e9569e2 Mon Sep 17 00:00:00 2001 From: Thanu Poptiphueng Date: Fri, 19 Apr 2024 18:18:16 +0700 Subject: [PATCH] add unique constraint --- src/schema.ts | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/schema.ts b/src/schema.ts index 293e2f3..be3f6ad 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -3,6 +3,7 @@ import { text, integer, primaryKey, + unique, } from "drizzle-orm/sqlite-core"; import { relations, sql } from "drizzle-orm"; @@ -41,7 +42,7 @@ export const userRelation = relations(user, ({ many, one }) => ({ //----------------Group export const group = sqliteTable("groups", { id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }), - name: text("name").notNull(), + name: text("name").unique().notNull(), }); export const groupRelation = relations(group, ({ many }) => ({ @@ -51,7 +52,7 @@ export const groupRelation = relations(group, ({ many }) => ({ //----------------Opinion export const opinion = sqliteTable("opinions", { id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }), - name: text("name").notNull(), + name: text("name").unique().notNull(), type: text("type", { enum: ["3Choice", "4Choice"] }) .default("3Choice") .notNull(), @@ -84,13 +85,17 @@ export const userOpinionRelation = relations(userOpinion, ({ one }) => ({ })); //----------------Zone -export const zone = sqliteTable("zones", { - id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }), - name: text("name").notNull(), - province: integer("province_id") - .notNull() - .references(() => province.id), -}); +export const zone = sqliteTable( + "zones", + { + id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }), + name: text("name").notNull(), + province: integer("province_id") + .notNull() + .references(() => province.id), + }, + (t) => ({ unique_name_province: unique().on(t.name, t.province) }) +); export const zoneRelation = relations(zone, ({ one }) => ({ province: one(province, { fields: [zone.province], @@ -101,7 +106,7 @@ export const zoneRelation = relations(zone, ({ one }) => ({ //----------------Province export const province = sqliteTable("provinces", { id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }), - name: text("name").notNull(), + name: text("name").unique().notNull(), }); export const provinceRelation = relations(province, ({ many }) => ({