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

PostgresError: cannot cast type boolean to jsonb #931

Open
mwannenmacher opened this issue Aug 19, 2024 · 0 comments
Open

PostgresError: cannot cast type boolean to jsonb #931

mwannenmacher opened this issue Aug 19, 2024 · 0 comments

Comments

@mwannenmacher
Copy link

Problem Description

Trying to insert [true, false] into a jsonb column like the following sql:

INSERT INTO public.device_events (id, device_id, event, information, timestamp) VALUES (gen_random_uuid(), '9ba9b367-97b1-4a6b-9403-9615d845304c', 'EXAMPLE_EVENT', '[true, false]'::jsonb, 0);

This query does work as expected directly in the db, but is throwing an exception when executed with this library.
We are receiving events from external devices and want to persist those events including event data directly into the db.

Expected Behaviour

Inserting column directly into the table with result

const event = {
    id: '1e25ebb1-9818-49e1-9d00-6477cd521036',
    deviceId: '9ba9b367-97b1-4a6b-9403-9615d845304c ',
    event: 'EXAMPLE_EVENT',
    information: [true, false],
    timestamp: 0
};
const result = await sql` INTO device_events (id, device_id, event, information, timestamp)
       VALUES (${event.id}::uuid,
            ${event.deviceId}::uuid,
            ${event.event}::event_types,
            ${(event.information)}::jsonb,
            ${event.timestamp})
       RETURNING *`;

result:

id device_id event information timestamp created_at 
5640d21c-9c81-4a3f-b411-380837003b8d 9ba9b367-97b1-4a6b-9403-9615d845304c EXAMPLE_EVENT [true, false] 0 2024-08-19 09:59:10.108266+00

Current Behaviour

const event = {
    id: '1e25ebb1-9818-49e1-9d00-6477cd521036',
    deviceId: '9ba9b367-97b1-4a6b-9403-9615d845304c ',
    event: 'EXAMPLE_EVENT',
    information: [true, false],
    timestamp: 0
};
const result = await sql` INTO device_events (id, device_id, event, information, timestamp)
       VALUES (${event.id}::uuid,
            ${event.deviceId}::uuid,
            ${event.event}::event_types,
            ${(event.information)}::jsonb,
            ${event.timestamp})
       RETURNING *`;

Error output:

    PostgresError: cannot cast type boolean to jsonb

Local fix

const event = {
    id: '1e25ebb1-9818-49e1-9d00-6477cd521036',
    deviceId: '9ba9b367-97b1-4a6b-9403-9615d845304c ',
    event: 'EXAMPLE_EVENT',
    // fixing error on mapping promitive boolean to Boolean object
    information: [true, false].map(v => new Boolean(v)),
    timestamp: 0
};
const result = await sql` INTO device_events (id, device_id, event, information, timestamp)
       VALUES (${event.id}::uuid,
            ${event.deviceId}::uuid,
            ${event.event}::event_types,
            ${(event.information)}::jsonb,
            ${event.timestamp})
       RETURNING *`;

Table Description

Table "public.device_events"

Column Type Collation Nullable Default
id uuid not null
device_id uuid not null
event event_types not null 'UNKNOWN'::event_types
information jsonb '{}'::jsonb
timestamp integer not null
created_at timestamp with time zone not null CURRENT_TIMESTAMP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant