Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum type #112

Closed
roitman-g opened this issue Jul 5, 2022 · 10 comments
Closed

Enum type #112

roitman-g opened this issue Jul 5, 2022 · 10 comments
Labels
api Related to library's API enhancement New feature or request postgres Related to PostgreSQL

Comments

@roitman-g
Copy link

I wonder if it is possible to create enum types with Kysely? I have not found anything in docs

@koskimas
Copy link
Member

koskimas commented Jul 6, 2022

Well, you can create anything with raw SQL using the sql template tag. You can run raw sql in migrations. On the typescript side enums work just fine. Just create an enum and use if for a field.

No enum support has been added yet to the schema builder because all database engines have different implementation for enums. Also people mean different things when they talk about enums. On postgres there's a way to create new enum types and use those as types for columns. AFAIK no other engine has something like that. Knex creates a check constraint for enums by default on postgres.

Kysely doesn't have an abstraction layer on top of SQL. Therefore there's never going to be an API like .addColumn('foo', 'text', col => col.enum(['a', 'b', 'c']) that would create different SQL on different engines.

There are already methods for creating check constraints, but you need to write the check code using raw sql.

What kind of support would you like see in kysely? And what do you mean by enum types?

@koskimas
Copy link
Member

koskimas commented Jul 7, 2022

I can add a CREATE TYPE "my_type" AS ENUM ('a', 'b', 'c') builder for postgres. That's definitely a useful addition.

@koskimas
Copy link
Member

koskimas commented Jul 7, 2022

On mysql enums are simple:

.addColumn('some_col', sql`enum('a', 'b', 'c', 'd', 'e')`)

You can even create a helper like this:

function enum(...args: string[]) {
  return sql`enum(${sql.join(args.map(sql.literal))})`
}

.addColumn('some_col', enum('a', 'b', 'c', 'd', 'e'))

@koskimas
Copy link
Member

koskimas commented Jul 8, 2022

@roitman-g Any thoughts?

@igalklebanov igalklebanov added enhancement New feature or request api Related to library's API postgres Related to PostgreSQL labels Oct 11, 2022
@vladstavarache
Copy link

vladstavarache commented May 4, 2023

For postgres you can do:

await db.schema
    .createType("status")
    .asEnum(["failed", "success"])
    .execute();

and then reference the status type:

.addColumn("status", sql`status`)

@bhgames
Copy link

bhgames commented May 18, 2023

When I do this, it throws a ts error saying that is not assignable to DataTypeExpression

@igalklebanov
Copy link
Member

@vladstavarache fixed your example with sql template tag usage in .addColumn.

@bhgames check the example again.

@leleofg
Copy link

leleofg commented May 19, 2023

the example above worked for me! I'm using postgres
thanks @vladstavarache

I created this pr to add enum in doc: #504

@anoopsimelabs
Copy link

@vladstavarache fixed your example with sql template tag usage in .addColumn.

@bhgames check the example again.

@igalklebanov , For me the following solution worked, when I want to use different name(UserStatus) for type.

await db.schema
    .createType("UserStatus")
    .asEnum(["failed", "success"])
    .execute();

and then reference the UserStatus type:

.addColumn("status", sql`"UserStatus"`)

@0xPT
Copy link

0xPT commented Jan 19, 2024

@anoopsimelabs @vladshcherbin how would you do migrations if you need to change the type? It doesn't seem like db.schema has any way to change the type. Is it better to just do raw SQL for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API enhancement New feature or request postgres Related to PostgreSQL
Projects
None yet
Development

No branches or pull requests

8 participants