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

[Feature Request] Check Constraint and Unique Constraint

Open Ericnr opened this issue 2 years ago • 3 comments

check

  • [x] ORM support
  • [ ] Kit support

unique

  • [ ] ORM support
  • [ ] Kit support

Would be great to see those implemented

Ericnr avatar Mar 07 '23 04:03 Ericnr

+1 to unique constraint

kelbyfaessler avatar Apr 02 '23 11:04 kelbyfaessler

I'm using drizzle-orm with sqlite, and typescript finds the export unique from drizzle-orm/sqlite-core, and looking at the type declarations, it looks like it exists.

However, is it not currently usable? I couldn't figure out how to actually use it, kept running into type errors.

ghost avatar Apr 10 '23 03:04 ghost

Any update on the unique constraint?

Proshashi avatar Jun 09 '23 16:06 Proshashi

yes, would love an update on unique constraint.

i'm using planetscale & foreign keys aren't supported.

the recommendation is to add @unique to userId... i'm not sure if using just index (userIdx) would do the trick:

export const key = mysqlTable(
  'auth_key',
  {
    id: varchar('id', {
      length: 255,
    }).primaryKey(),
    userId: varchar('user_id', {
      length: 15,
    }).notNull(),
    primaryKey: boolean('primary_key').notNull(),
    hashedPassword: varchar('hashed_password', {
      length: 255,
    }),
    expires: bigint('expires', {
      mode: 'number',
    }),
  },
  (table) => {
    return {
      userIdx: index('user_idx').on(table.userId),
    }
  }
)

deadcoder0904 avatar Jun 15 '23 14:06 deadcoder0904

til; @algora-pbc isn't good with handling decimals

MurmeltierS avatar Jun 19 '23 18:06 MurmeltierS

hey @MurmeltierS, your $0.50 bounty didn't go through as we have a $10 limit - just updated the bounty form to reflect that. you can create a new bounty if you'd like. cheers

zcesur avatar Jun 19 '23 18:06 zcesur

Does anyone know if it's okay to manually add the UNIQUE constraint to the migration file before running it? I think this should work okay while the feature is added to Drizzle, but I haven't seen the suggestion anywhere, so I wonder if it may have some unwanted side effects. Anyone using this approach? Or any thoughts in the matter? thanks ✌🏻

medromo avatar Jun 21 '23 01:06 medromo

We care about unique constraint, which is a very important part of the database. Any progress?

AprilNEA avatar Jun 23 '23 19:06 AprilNEA

Any updates on unique constraints? Or workarounds? Has to be one of the most-requested features

aetherplex avatar Jun 24 '23 23:06 aetherplex

💎 $40 bounty created by @MurmeltierS 👉 To claim this bounty, submit your pull request on Algora 📝 Before proceeding, please make sure you can receive payouts in your country 💵 Payment arrives in your account 2-5 days after the bounty is rewarded 💯 You keep 100% of the bounty award 🙏 Thank you for contributing to drizzle-team/drizzle-orm!

algora-pbc avatar Jun 27 '23 15:06 algora-pbc

@MurmeltierS, I'm attempting this.

jemiluv8 avatar Jun 29 '23 12:06 jemiluv8

https://github.com/drizzle-team/drizzle-orm/pull/825 orm part is done now, I need to finish drizzle-kit part for migrations

AndriiSherman avatar Jun 29 '23 12:06 AndriiSherman

Ok. @AndriiSherman. Thanks for the timely update. I will no longer be attempting this.

jemiluv8 avatar Jun 29 '23 12:06 jemiluv8

Kit part is done. I'm performing the final tests and hoping to release the unique constraints tomorrow. By the way, after extensively researching the documentation, I discovered that unique indexes should be identical to unique constraints.

According to the SQLite documentation, unique constraints are treated as unique indexes. Therefore, in the ORM and Kit, we will handle unique constraints for SQLite as unique indexes, providing a more convenient syntax sugar

image

I couldn't find any reliable information about the differences in MySQL. The only distinction I found in PostgreSQL is that unique constraints can ensure proper uniqueness in transaction modes or parallel queries. I will attempt to replicate this behavior locally.

So essentially, unique indexes should suffice for your needs. However, this task is already completed, and it was done in order to accommodate both syntaxes for each database

But for all databases, constraints create unique indexes for you and utilize them for uniqueness detection. It's essentially the same as simply creating a unique index, which we already have in Drizzle

AndriiSherman avatar Jul 10 '23 20:07 AndriiSherman

Everything is ready to be checked on [email protected] and [email protected] Closing this issue as completed and creating a new one just for checks constraints support

AndriiSherman avatar Jul 11 '23 22:07 AndriiSherman

🎉🎈 @drizzle-team has been awarded $40 by @MurmeltierS! 🎈🎊

algora-pbc avatar Jul 12 '23 13:07 algora-pbc