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

Implement `check` constraint in drizzle-kit

Open dankochetov opened this issue 2 years ago • 11 comments

dankochetov avatar Mar 24 '23 08:03 dankochetov

What is the status of check constraints? I'm trying to implement exclusive subtypes. Any recommendations as to how to approach this would be greatly appreciated. Thanks!

Exclusive subtypes

SebastianGarces avatar Jul 24 '23 19:07 SebastianGarces

Unsure if this is the implementation and it's just not documented?

https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/pg-core/checks.ts

Am I understanding that I have to use the check function and provide a SQL string?

SebastianGarces avatar Jul 24 '23 19:07 SebastianGarces

Would love to see this documented as well

austinm911 avatar Aug 15 '23 14:08 austinm911

Check API is implemented on the ORM side, but the Kit side is still pending, which is required for the feature to be usable.

dankochetov avatar Sep 04 '23 12:09 dankochetov

I've renamed the issue to be able to track the feature in general, not just the documentation for it.

dankochetov avatar Sep 04 '23 12:09 dankochetov

Hey @dankochetov! I am wondering if someone can update the documentation for CHECK if it has already been implemented on the ORM side? Thanks!

itsjoeoui avatar Dec 29 '23 20:12 itsjoeoui

There's probably a lot of devs that have worked with SQL and never used CHECK, because it's almost never supported by ORMs. I'm currently using them with drizzle to model polymorphic relationship, which works great in Postgres :

ALTER TABLE "Like"
  ADD CONSTRAINT check_like_polymorphism
  CHECK (num_nonnulls("pictureId", "videoId", "songId") = 1);

This would enforce that only one of these 3 columns have a value while the other 2 must be null. Would be nice to document this pattern once kit support is in.

Hebilicious avatar Jan 15 '24 23:01 Hebilicious

There's probably a lot of devs that have worked with SQL and never used CHECK, because it's almost never supported by ORMs. I'm currently using them with drizzle to model polymorphic relationship, which works great in Postgres :

ALTER TABLE "Like"
  ADD CONSTRAINT check_like_polymorphism
  CHECK (num_nonnulls("pictureId", "videoId", "songId") = 1);

This would enforce that only one of these 3 columns have a value while the other 2 must be null. Would be nice to document this pattern once kit support is in.

@Hebilicious, Did you declare this constraint on the schema? I tried to add a RAW SQL like this, but when I ran the generate command, the schema was generated without constraint.

leandromatos avatar Mar 20 '24 12:03 leandromatos

There's probably a lot of devs that have worked with SQL and never used CHECK, because it's almost never supported by ORMs. I'm currently using them with drizzle to model polymorphic relationship, which works great in Postgres :

ALTER TABLE "Like"
  ADD CONSTRAINT check_like_polymorphism
  CHECK (num_nonnulls("pictureId", "videoId", "songId") = 1);

This would enforce that only one of these 3 columns have a value while the other 2 must be null. Would be nice to document this pattern once kit support is in.

@Hebilicious, Did you declare this constraint on the schema? I tried to add a RAW SQL like this, but when I ran the generate command, the schema was generated without constraint.

You must run the raw SQL manually, which can be done by editing a drizzle kit migration or creating a custom one. There's nothing in the drizzle schema though, so I add a comment in the table definition saying the check exists for now.

Hebilicious avatar Mar 20 '24 13:03 Hebilicious

I just realized it is really simple to achieve that. I already wrote a seeder class that executes operations through transactions. To add RAW SQL into the database, I can use the same approach, which is a dedicated class to run RAW SQLs.

leandromatos avatar Apr 16 '24 16:04 leandromatos

 (table) => ({
    ageCheck: check('age_check', sql`${table.age} >= 0`)
  }),

Does the API look like so?

tobychidi avatar Aug 10 '24 21:08 tobychidi

Available in [email protected] and [email protected]

AndriiSherman avatar Oct 15 '24 19:10 AndriiSherman