Inserting into columns of type Enum

Hi! I am not sure how to insert data into enum column type...
export const batchTransactionTypeEnum = pgEnum("type", [
"IN",
"OUT"
]);

export const batchTransaction = pgTable('batch_transaction', {
id: serial('id').primaryKey(),
type: batchTransactionTypeEnum("type"),
})
export const batchTransactionTypeEnum = pgEnum("type", [
"IN",
"OUT"
]);

export const batchTransaction = pgTable('batch_transaction', {
id: serial('id').primaryKey(),
type: batchTransactionTypeEnum("type"),
})
and query:
let bt = await tx.insert(batchTransaction).values({
batchId: b.id,
type: "IN",
quantityInBaseUnit: ds.document_subline.quantityInBaseUnit,
});
let bt = await tx.insert(batchTransaction).values({
batchId: b.id,
type: "IN",
quantityInBaseUnit: ds.document_subline.quantityInBaseUnit,
});
above code results in ungly error:
Query: insert into "batch_transaction" ("id", "quantity_in_base_unit", "type", "created_at", "batch_id") values (default, $1, $2, default, $3) -- params: ["25.00", "IN", 9]
H:\projects\wms\backend-express\node_modules\postgres\cjs\src\connection.js:790
const error = Errors.postgres(parseError(x))
^
PostgresError: nieprawidłowa wartość wejścia dla enumeracji type: "IN"
at ErrorResponse (H:\projects\wms\backend-express\node_modules\postgres\cjs\src\connection.js:790:26)
at handle (H:\projects\wms\backend-express\node_modules\postgres\cjs\src\connection.js:476:6)
at Socket.data (H:\projects\wms\backend-express\node_modules\postgres\cjs\src\connection.js:315:9)
at Socket.emit (node:events:514:28)
at Socket.emit (node:domain:489:12)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Socket.Readable.push (node:internal/streams/readable:234:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
severity_local: 'BŁĄD',
severity: 'ERROR',
code: '22P02',
where: "unnamed portal parameter $2 = '...'",
file: 'enum.c',
line: '133',
routine: 'enum_in'
}
Query: insert into "batch_transaction" ("id", "quantity_in_base_unit", "type", "created_at", "batch_id") values (default, $1, $2, default, $3) -- params: ["25.00", "IN", 9]
H:\projects\wms\backend-express\node_modules\postgres\cjs\src\connection.js:790
const error = Errors.postgres(parseError(x))
^
PostgresError: nieprawidłowa wartość wejścia dla enumeracji type: "IN"
at ErrorResponse (H:\projects\wms\backend-express\node_modules\postgres\cjs\src\connection.js:790:26)
at handle (H:\projects\wms\backend-express\node_modules\postgres\cjs\src\connection.js:476:6)
at Socket.data (H:\projects\wms\backend-express\node_modules\postgres\cjs\src\connection.js:315:9)
at Socket.emit (node:events:514:28)
at Socket.emit (node:domain:489:12)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Socket.Readable.push (node:internal/streams/readable:234:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
severity_local: 'BŁĄD',
severity: 'ERROR',
code: '22P02',
where: "unnamed portal parameter $2 = '...'",
file: 'enum.c',
line: '133',
routine: 'enum_in'
}
Found simmilar topic, but solution with adding cosnt to enum value does not work. https://discord.com/channels/1043890932593987624/1120919676457848946
4 Replies
tzezar
tzezarOP10mo ago
translated error
invalid input value for enumeration type: 'IN'
invalid input value for enumeration type: 'IN'
Angelelz
Angelelz10mo ago
Isn't this an error coming from the database? You might have not applied a migration, or the push didn't work. Basically the database is complaining the 'IN' is not a valid value for that column
tzezar
tzezarOP10mo ago
Exactly; but I have no idea why this is a case. Migrations were done, table definition seems to be fine, insert statement also. I changed column definition from pgEnum to text type (as below) and it worked just fine
type: text("type", { enum: ['IN', 'OUT'] }).notNull()
type: text("type", { enum: ['IN', 'OUT'] }).notNull()
Angelelz
Angelelz10mo ago
If that shows the type that you like and it works, there you go
Want results from more Discord servers?
Add your server