kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.33k stars 262 forks source link

Updating column data type does not allow custom data types with SQL expression #474

Closed hannesj closed 1 year ago

hannesj commented 1 year ago

This is a small issue with the ts typings.

The following works:

sql`create domain email as citext`.execute(db);

db.schema
    .createTable("email")
    .addColumn("email", sql`email`, (col) => col.primaryKey())
    .execute();

But the following is not allowed:

sql`create domain email as citext`.execute(db);

db.schema
    .alterTable("email")
    .alterColumn("email", (col) => col.setDataType(sql`email`))
    .execute();

The second argument of addColumn is a DataTypeExpression which is ColumnDataType | Expression<any>, while the argument for setDataType is just ColumnDataType

ImLunaHey commented 1 year ago

Did this make it into a release as this doesn't seem to be working for me. I'm still getting a typescript error on the argument I'm passing to alterColumn.

export const ModerationReasons = [
    'SPAM',
    'INACTIVE',
    'CUSTOM'
] as const;
export const up = async (db: Kysely<unknown>) => {
    // Moderation
    await db.schema
        .alterTable('moderation')
        .alterColumn('reason', col => col.setDataType(sql`enum(${sql.join(Object.values(ModerationReasons).map(arg => sql.lit(arg)))})`))
        .execute();
};

This works but the above doesn't.

export const up = async (db: Kysely<unknown>) => {
    // Moderation
    await db.schema
        .createTable('moderation')
        .ifNotExists()
        .addColumn('id', 'varchar(36)', (col) => col.defaultTo(sql`(uuid())`).primaryKey().notNull())
        .addColumn('reason', sql`enum(${sql.join(Object.values(ModerationReasons).map(arg => sql.lit(arg)))})`, col => col.notNull())
        .execute();
};
igalklebanov commented 1 year ago

@hannesj @ImLunaHey this has been released.