K
Kysely•2y ago
Nil

I am getting an error when entering an id with a uuid value

const uuid = uuidv4() await db .withSchema("schema_name") .insertInto("table_name") .values({ id: uuid }) .execute(); Error: ERROR: column "id" is of type uuid but expression is of type character varying Hint: You will need to rewrite or cast the expression.
Solution:
Hey 👋 You should try casting it to uuid using sql template tag: ```ts...
Jump to solution
4 Replies
Solution
Igal
Igal•2y ago
Hey 👋 You should try casting it to uuid using sql template tag:
import { sql } from 'kysely';

const uuid = uuidv4()

await db
.withSchema("schema_name")
.insertInto("table_name")
.values({
id: sql`${uuid}::uuid`
})
.execute();
import { sql } from 'kysely';

const uuid = uuidv4()

await db
.withSchema("schema_name")
.insertInto("table_name")
.values({
id: sql`${uuid}::uuid`
})
.execute();
Nil
NilOP•2y ago
wow, thanks, it's worked!
koskimas
koskimas•2y ago
If you have the option, I'd suggest using TEXT instead of UUID as the data type in the DB. In my experience, the UUID data type causes more trouble than it's worth With TEXT you use 36 bytes per uuid value as with UUID you only use 16. But that's pretty much the only benefit of using UUID nowadays. And disk space is cheap.
Nil
NilOP•2y ago
I found it very difficult to work with him, thanks for the help!

Did you find this page helpful?