Make varchar field one of string union
I know that CHECK constraint hasn't been implemented yet, but I want to implement a Role field that is a varchar that can only be set to "user", "paid-user", or "admin" and I want the schema definition in TS to reflect this.
I know this wont set the constraint at the DB level, but I want to TS definition to reflect that only the above union of strings are valid. How can I tell drizzle that this field should be this specific TS type?
4 Replies
You can declare enum for that. https://orm.drizzle.team/docs/sql-schema-declaration
Drizzle ORM - Overview
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Yes, you can add
enum
field for varchar
, but it won’t throw an error(only types) when you inserting if the value is wrong.
https://orm.drizzle.team/docs/column-types/pg#varchar
Or you can create pgEnum
/mysqlEnum
https://orm.drizzle.team/docs/column-types/pg#enum
https://orm.drizzle.team/docs/column-types/mysql#enumcolumnDrizzle ORM - PostgreSQL column types
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Drizzle ORM - MySQL column types
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Thank you both! I am choosing to use the
pgEnum
feature.
An enum is saved in the DB as an integer, correct? I'm using postgres...That's saved as a string.