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

How to compare a UUID field #97

Closed
t-indie opened this issue May 22, 2022 · 1 comment
Closed

How to compare a UUID field #97

t-indie opened this issue May 22, 2022 · 1 comment
Labels
api Related to library's API help wanted Extra attention is needed postgres Related to PostgreSQL

Comments

@t-indie
Copy link

t-indie commented May 22, 2022

Firstly, thank you so much for maintaining this project and providing the insightful example

I have a PostgreSQL users table where the id is of type UUID, however when I use the where comparator I get this error "operator does not exist: uuid = character varying"

export async function findUserById(
    db: Kysely<Database>,
    id: string
): Promise<UserRow | undefined> {
    const user = await db
        .selectFrom(tableName)
        .where('id', '=', id)
        .selectAll(tableName)
        .executeTakeFirst();
    return user;
}

I found online that one should use this operator but unfortunately it throws an error

.where('id::text', '=', id)

Any ideas would be appreciated. Thanks in advance

@koskimas
Copy link
Member

koskimas commented May 23, 2022

Either like this

.where(sql`id::text`, '=', id)

or like this

function asUuid(val: string): RawBuilder<string> {
  return sql`${val}::uuid`
}
.where('id', '=', asUuid(id))

@igalklebanov igalklebanov added help wanted Extra attention is needed api Related to library's API postgres Related to PostgreSQL labels Oct 11, 2022
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 help wanted Extra attention is needed postgres Related to PostgreSQL
Projects
None yet
Development

No branches or pull requests

3 participants