Cannot get enums to work with query_params
Looskie opened this issue · 2 comments
Looskie commented
When I use query_params
with an enum, it does not work.
Steps to reproduce
- Create a local ch server and run the table query
- Connect
- run the code example below
Expected behaviour
to work :D
Code example
await client.command({
query: `INSERT INTO default.customers (id, customer_name, is_admin, created_at, random_enum, pi, card_number, uuid) VALUES ({v0: String}, {v1: String}, {v2: Boolean}, {v3: DateTime64}, {v4: Enum8}, {v5: Float32}, {v6: Int32}, {v7: UUID})`,
query_params: {
v0: 'd65d104b-8398-419a-9b7f-87d13872d5f7',
v1: 'John Doe',
v2: false,
v3: new Date(),
v4: 'A',
v5: 3.14,
v6: 123456789,
v7: '123'
}
})
Error log
Enum data type cannot be empty: While executing ValuesBlockInputFormat.
Configuration
Environment
- Client version: 0.2.5
- Language version: v20.5.0
- OS: 14.0 (23A339)
ClickHouse server
- ClickHouse Server version: 23.7.5
CREATE TABLE
statements for tables involved:
CREATE TABLE IF NOT EXISTS default.customers
(
id String,
customer_name String,
is_admin Boolean,
created_at DateTime64,
random_enum Enum8('A' = 0, 'B' = 1, 'C' = 2),
pi Float32,
card_number Int32,
uuid UUID
)
ENGINE = MergeTree()
PRIMARY KEY id
I feel like I'm missing something so obvious
slvrtrn commented
There are several ways to bind an enum.
Either send a string representation of your enum (A
/B
/C
) and bind as a String
, index (0/1/2) and bind as a UInt8
, or provide an entire type Enum8('A' = 0, 'B' = 1, 'C' = 2)
(it complaints about precisely that, Enum8
!= Enum8('A' = 0, 'B' = 1, 'C' = 2)
).
See also:
EDIT: this works.
await client.command({
query: `INSERT INTO default.customers (id, customer_name, is_admin, created_at, random_enum, pi, card_number, uuid)
VALUES ({v0: String}, {v1: String}, {v2: Boolean}, {v3: DateTime64}, {v4: String}, {v5: Float32},
{v6: Int32}, {v7: UUID})`,
query_params: {
v0: 'd65d104b-8398-419a-9b7f-87d13872d5f7',
v1: 'John Doe',
v2: false,
v3: new Date(),
v4: 'A',
v5: 3.14,
v6: 123456789,
v7: crypto.randomUUID(),
},
})
Looskie commented
sweet, thank you so much for the quick answer ❤️