DrizzleORM v0.27.2 릴리스
Jul 12, 2023
🎉 PostgreSQL, MySQL, SQLite에서 UNIQUE
제약 조건 지원 추가
PostgreSQL에서는 단일 컬럼에 대한 고유 제약 조건은 컬럼 레벨에서 정의할 수 있으며, 다중 컬럼 제약 조건은 세 번째 매개변수에서 정의할 수 있습니다. 두 경우 모두 제약 조건에 커스텀 이름을 지정할 수 있습니다. 또한 PostgreSQL은 NULLS NOT DISTINCT
옵션을 지원하여 테이블에서 하나 이상의 NULL 값을 허용하지 않도록 제한할 수 있습니다. 참고
다음은 unique
사용법을 보여주는 예제입니다. 실제 사용 사례를 찾으려고 하지 마세요.
// 단일 컬럼
const table = pgTable('table', {
id: serial('id').primaryKey(),
name: text('name').notNull().unique(),
state: char('state', { length: 2 }).unique('custom'),
field: char('field', { length: 2 }).unique('custom_field', { nulls: 'not distinct' }),
});
// 다중 컬럼
const table = pgTable('table', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
state: char('state', { length: 2 }),
}, (t) => ({
first: unique('custom_name').on(t.name, t.state).nullsNotDistinct(),
second: unique('custom_name1').on(t.name, t.state),
}));
MySQL
MySQL에서는 NULLS NOT DISTINCT
옵션을 제외하고 모든 것이 동일합니다. MySQL은 이 옵션을 지원하지 않는 것으로 보입니다.
다음은 unique
사용법을 보여주는 예제입니다. 실제 사용 사례를 찾으려고 하지 마세요.
// 단일 컬럼
const table = mysqlTable('table', {
id: serial('id').primaryKey(),
name: text('name').notNull().unique(),
state: text('state').unique('custom'),
field: text('field').unique('custom_field'),
});
// 다중 컬럼
const table = mysqlTable('cities1', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
state: text('state'),
}, (t) => ({
first: unique().on(t.name, t.state),
second: unique('custom_name1').on(t.name, t.state),
}));
SQLite
SQLite에서 고유 제약 조건은 고유 인덱스와 동일합니다. SQLite에서 고유 인덱스에 이름을 지정할 수 있는 한, 내부 구현에서는 모든 고유 제약 조건을 고유 인덱스로 처리합니다.
// 단일 컬럼
const table = sqliteTable('table', {
id: int('id').primaryKey(),
name: text('name').notNull().unique(),
state: text('state').unique('custom'),
field: text('field').unique(),
});
// 다중 컬럼
const table = sqliteTable('table', {
id: int('id').primaryKey(),
name: text('name').notNull(),
state: text('state'),
}, (t) => ({
first: unique().on(t.name, t.state),
second: unique('custom').on(t.name, t.state),
}));