MySQL 从 8.0.13
版本开始支持功能索引。正确的语法是,表达式应该用括号括起来,例如 (lower(column))
。
Drizzle | 唯一值且不区分大小写的邮件处理
This guide assumes familiarity with:
PostgreSQL
要使用 Drizzle 在 PostgreSQL 中实现唯一且不区分大小写的 email
处理,你可以在小写的 email
列上创建唯一索引。这样,无论大小写如何,你都可以确保 email
都是唯一的。
Drizzle 拥有简单灵活的 API,可让你使用类似 SQL 的语法轻松创建索引:
schema.ts
migration.sql
import { SQL, sql } from 'drizzle-orm';
import { AnyPgColumn, pgTable, serial, text, uniqueIndex } from 'drizzle-orm/pg-core';
export const users = pgTable(
'users',
{
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
},
(table) => [
// uniqueIndex('emailUniqueIndex').on(sql`lower(${table.email})`),
uniqueIndex('emailUniqueIndex').on(lower(table.email)),
],
);
// custom lower function
export function lower(email: AnyPgColumn): SQL {
return sql`lower(${email})`;
}
以下是如何使用 lower
函数通过 email
选择用户:
import { eq } from 'drizzle-orm';
import { lower, users } from './schema';
const db = drizzle(...);
const findUserByEmail = async (email: string) => {
return await db
.select()
.from(users)
.where(eq(lower(users.email), email.toLowerCase()));
};
select * from "users" where lower(email) = 'john@email.com';
MySQL
在 MySQL 中,字符串比较的默认排序规则设置不区分大小写,这意味着在 SQL 查询中执行搜索或比较字符串等操作时,字符的大小写不会影响结果。但是,由于排序规则设置可能有所不同,并且可能配置为区分大小写,因此我们将通过在小写的 email
列上创建唯一索引来明确确保 email
是唯一的,无论大小写如何。
Drizzle 拥有简单灵活的 API,可让你使用类似 SQL 的语法轻松创建索引:
schema.ts
migration.sql
import { SQL, sql } from 'drizzle-orm';
import { AnyMySqlColumn, mysqlTable, serial, uniqueIndex, varchar } from 'drizzle-orm/mysql-core';
export const users = mysqlTable(
'users',
{
id: serial('id').primaryKey(),
name: varchar('name', { length: 255 }).notNull(),
email: varchar('email', { length: 255 }).notNull(),
},
(table) => [
// uniqueIndex('emailUniqueIndex').on(sql`(lower(${table.email}))`),
uniqueIndex('emailUniqueIndex').on(lower(table.email)),
]
);
// custom lower function
export function lower(email: AnyMySqlColumn): SQL {
return sql`(lower(${email}))`;
}
IMPORTANT
以下是如何使用 lower
函数通过 email
选择用户:
import { eq } from 'drizzle-orm';
import { lower, users } from './schema';
const db = drizzle(...);
const findUserByEmail = async (email: string) => {
return await db
.select()
.from(users)
.where(eq(lower(users.email), email.toLowerCase()));
};
select * from `users` where lower(email) = 'john@email.com';
SQLite
要使用 Drizzle 在 SQLite 中实现唯一且不区分大小写的 email
处理,你可以在小写的 email
列上创建唯一索引。这样,无论大小写如何,你都可以确保 email
都是唯一的。
Drizzle 拥有简单灵活的 API,可让你使用类似 SQL 的语法轻松创建索引:
schema.ts
migration.sql
import { SQL, sql } from 'drizzle-orm';
import { AnySQLiteColumn, integer, sqliteTable, text, uniqueIndex } from 'drizzle-orm/sqlite-core';
export const users = sqliteTable(
'users',
{
id: integer('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
},
(table) => [
// uniqueIndex('emailUniqueIndex').on(sql`lower(${table.email})`),
uniqueIndex('emailUniqueIndex').on(lower(table.email)),
]
);
// custom lower function
export function lower(email: AnySQLiteColumn): SQL {
return sql`lower(${email})`;
}
以下是如何使用 lower
函数通过 email
选择用户:
import { eq } from 'drizzle-orm';
import { lower, users } from './schema';
const db = drizzle(...);
const findUserByEmail = async (email: string) => {
return await db
.select()
.from(users)
.where(eq(lower(users.email), email.toLowerCase()));
};
select * from "users" where lower(email) = 'john@email.com';