drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: there is no unique or exclusion constraint matching the ON CONFLICT specification

Open ghyath5 opened this issue 1 year ago • 3 comments

What version of drizzle-orm are you using?

0.32.0

What version of drizzle-kit are you using?

0.23.0

Describe the Bug

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

Expected behavior

No response

Environment & setup

No response

ghyath5 avatar Jul 17 '24 08:07 ghyath5

i have a similar issue

drewlev avatar Jul 28 '24 16:07 drewlev

@drewlev Yeah this is an urgent issue to be resolved It's not difficult to be handled

ghyath5 avatar Jul 29 '24 10:07 ghyath5

Also facing this issue

aidansunbury avatar Aug 02 '24 18:08 aidansunbury

Closing in favor of #2646.

L-Mario564 avatar Oct 23 '24 17:10 L-Mario564