import { sql, type SQL } from "drizzle-orm";
import type { AnyPgColumn } from "drizzle-orm/pg-core";
export function lower(col: AnyPgColumn): SQL {
return sql`lower(${col})`;
}
users.ts schema:
export const users = pgTable(
"users",
{
id: text("id")
.primaryKey()
.$defaultFn(() => createId()),
name: varchar("name", { length: 255 }).notNull(),
slug: varchar("slug", { length: 200 }), // Specified length
email: varchar("email", { length: 255 }).notNull()
},
(table) => {
return {
slugUniqueIndex: uniqueIndex("user_slug_unique_idx").on(
lower(table.slug)
),
emailUniqueIndex: uniqueIndex("user_email_unique_idx").on(
lower(table.email)
),
};
}
);
Upsert user query:
const [createdUser] = await tx
.insert(users)
.values({
name: user.name,
email: user.email,
image: user.picture,
})
.onConflictDoNothing({
target: users.email,
})
.returning({
slug: users.slug,
id: users.id,
email: users.email,
});
console.log(createdUser);
I know it was defined as Lower email
I tried multiple solutions with no luck
.onConflictDoNothing({
target: [users.email],
})
.onConflictDoNothing({
target: [lower(users.email)],
})`
`Type 'SQL<unknown>' is missing the following properties from type 'PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>': table, name, primary, notNull`
`.onConflictDoNothing({
target: sql`lower(${users.email})`,
})`
Type ‘SQL’ is not assignable to type ‘IndexColumn | IndexColumn[] |
undefined’. Type ‘SQL’ is missing the following properties from type
‘PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>’: table,
name, primary, notNull
Although these were ts errors I tried the query also it failed and didn’t work