DT
Drizzle TeamBaron Von Enablestein

pgEnum conversion not working as expected

I have the following pgEnum defined:
export const ApprovalRequestTypePgEnum = pgEnum('approval_request_type', ['CREDIT_NOTE']);
export const ApprovalRequestTypePgEnum = pgEnum('approval_request_type', ['CREDIT_NOTE']);
(Yes, there'll be more types later). My schema is:
export const approvalRequestsTable = pgTable('approval_requests', {
id: uuid('id').default(sql`gen_random_uuid()`).primaryKey().notNull(),
requestorId: integer('requestor_id').notNull().references(() => employeesTable.workerId),
type: ApprovalRequestTypePgEnum('type').notNull(),
approvalObjectId: uuid('approval_object_id').notNull(),
status: ApprovalStatusPgEnum('status').notNull(),
totalValue: integer('total_value').notNull(),
currencyCode: text('currency_code').notNull(),
});
export const approvalRequestsTable = pgTable('approval_requests', {
id: uuid('id').default(sql`gen_random_uuid()`).primaryKey().notNull(),
requestorId: integer('requestor_id').notNull().references(() => employeesTable.workerId),
type: ApprovalRequestTypePgEnum('type').notNull(),
approvalObjectId: uuid('approval_object_id').notNull(),
status: ApprovalStatusPgEnum('status').notNull(),
totalValue: integer('total_value').notNull(),
currencyCode: text('currency_code').notNull(),
});
However when I do:
export const insertApprovalRequest = async (approvalRequest: ApprovalRequestInput): Promise<ApprovalRequest> => {
logger.info('Inserting approval request', { approvalRequest });
const insertedApprovalRequest = await db
.insert(approvalRequestsTable).values({
...approvalRequest,
type: ApprovalRequestType.CREDIT_NOTE
}).returning().then((res) => res[0]);
logger.info('Inserted approval request', { insertedApprovalRequest });

return getApprovalRequest(insertedApprovalRequest.id) as Promise<ApprovalRequest>;
};
export const insertApprovalRequest = async (approvalRequest: ApprovalRequestInput): Promise<ApprovalRequest> => {
logger.info('Inserting approval request', { approvalRequest });
const insertedApprovalRequest = await db
.insert(approvalRequestsTable).values({
...approvalRequest,
type: ApprovalRequestType.CREDIT_NOTE
}).returning().then((res) => res[0]);
logger.info('Inserted approval request', { insertedApprovalRequest });

return getApprovalRequest(insertedApprovalRequest.id) as Promise<ApprovalRequest>;
};
I get the error ERROR: column \"type\" is of type approval_request_type but expression is of type text; Hint: You will need to rewrite or cast the expression. How can I solve this? My ApprovalRequestType enum is also mapped to 'CREDIT_NOTE'
Baron Von Enablestein
Baron Von Enablestein18d ago
Hmmm, if I look at the full stack trace this might have to do with me using the RDS Data API:
{
"name": "DatabaseErrorException",
"location": "/var/runtime/node_modules/@aws-sdk/client-rds-data/dist-cjs/index.js:871",
"message": "ERROR: column \"type\" is of type approval_request_type but expression is of type text; Hint: You will need to rewrite or cast the expression.; Position: 154; SQLState: 42804",
"stack": "DatabaseErrorException: ERROR: column \"type\" is of type approval_request_type but expression is of type text; Hint: You will need to rewrite or cast the expression.; Position: 154; SQLState: 42804\n at de_DatabaseErrorExceptionRes (/var/runtime/node_modules/@aws-sdk/client-rds-data/dist-cjs/index.js:871:21)\n at de_CommandError (/var/runtime/node_modules/@aws-sdk/client-rds-data/dist-cjs/index.js:791:19)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20\n at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/core/dist-cjs/index.js:165:18\n at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38\n at async /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:22\n at async AwsDataApiPreparedQuery.values (/var/task/index.js:25140:20)\n at async AwsDataApiPreparedQuery.execute (/var/task/index.js:25111:20)\n at async insertApprovalRequest (/var/task/index.js:29434:35)"
}
{
"name": "DatabaseErrorException",
"location": "/var/runtime/node_modules/@aws-sdk/client-rds-data/dist-cjs/index.js:871",
"message": "ERROR: column \"type\" is of type approval_request_type but expression is of type text; Hint: You will need to rewrite or cast the expression.; Position: 154; SQLState: 42804",
"stack": "DatabaseErrorException: ERROR: column \"type\" is of type approval_request_type but expression is of type text; Hint: You will need to rewrite or cast the expression.; Position: 154; SQLState: 42804\n at de_DatabaseErrorExceptionRes (/var/runtime/node_modules/@aws-sdk/client-rds-data/dist-cjs/index.js:871:21)\n at de_CommandError (/var/runtime/node_modules/@aws-sdk/client-rds-data/dist-cjs/index.js:791:19)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20\n at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/core/dist-cjs/index.js:165:18\n at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38\n at async /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:22\n at async AwsDataApiPreparedQuery.values (/var/task/index.js:25140:20)\n at async AwsDataApiPreparedQuery.execute (/var/task/index.js:25111:20)\n at async insertApprovalRequest (/var/task/index.js:29434:35)"
}