drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too πŸ˜…
https://orm.drizzle.team
Apache License 2.0
24.77k stars 656 forks source link

[BUG]: Cannot run drizzle-studio with DATA-API. Unsupported data type "CHAR" #3273

Open valentinbeggi opened 4 weeks ago

valentinbeggi commented 4 weeks ago

What version of drizzle-orm are you using?

0.34.1

What version of drizzle-kit are you using?

0.25.0

Describe the Bug

It was working fine last week but now, when trying to run the studio, I'm getting this error.

Error: The result contains the unsupported data type "CHAR". at _ (https://local.drizzle.studio/index.js:14138:33349) at async https://local.drizzle.studio/index.js:14138:33539 at async Promise.all (index 1) at async $oi (https://local.drizzle.studio/index.js:13940:1587) at async Xkl (https://local.drizzle.studio/index.js:14138:33520)

I managed to debug and get the query responsible for this error. I reproduce this error within the query editor of RDS

SELECT 
    CASE 
        WHEN c.relkind = 'r' THEN 'table'
        WHEN c.relkind = 'v' THEN 'view'
        WHEN c.relkind = 'm' THEN 'mat_view'
    END AS entity_type,
    n.nspname AS table_schema,
    c.relname AS table_name,
    a.attname AS column_name,
    pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
    CASE a.attnotnull 
        WHEN TRUE THEN 'NO' 
        ELSE 'YES' 
    END AS is_nullable,
    CASE 
        WHEN pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) LIKE 'nextval(%' 
             AND format_type(a.atttypid, a.atttypmod) = 'integer' THEN 'serial'
        WHEN pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) LIKE 'nextval(%' 
             AND format_type(a.atttypid, a.atttypmod) = 'bigint' THEN 'bigserial'
        WHEN pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) LIKE 'nextval(%' 
             AND format_type(a.atttypid, a.atttypmod) = 'smallint' THEN 'smallserial'
        ELSE format_type(a.atttypid, a.atttypmod)
    END AS data_type,
    CASE 
        WHEN t.typcategory = 'A' THEN 'ARRAY'
        WHEN t.typtype = 'b' THEN a.atttypid::regtype::text
        ELSE 'USER-DEFINED'
    END AS additional_dt,
    a.attndims AS array_dimensions,
    a.attnum,
    a.attidentity AS identity_type,
    a.attgenerated AS generated_type,
    seq_class.relname AS identity_name,
    seq.seqincrement AS identity_increment,
    seq.seqmax AS identity_max,
    seq.seqmin AS identity_min,
    seq.seqstart AS identity_start,
    seq.seqcache AS identity_cache,
    seq.seqcycle AS identity_cycle
FROM 
    pg_catalog.pg_attribute a
JOIN 
    pg_catalog.pg_class c ON a.attrelid = c.oid
JOIN 
    pg_catalog.pg_namespace n ON c.relnamespace = n.oid
LEFT JOIN 
    pg_catalog.pg_depend dep ON dep.refobjid = c.oid AND dep.refobjsubid = a.attnum
LEFT JOIN 
    pg_catalog.pg_class seq_class ON seq_class.oid = dep.objid
LEFT JOIN 
    pg_catalog.pg_sequence seq ON seq_class.oid = seq.seqrelid
LEFT JOIN 
    pg_catalog.pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
JOIN 
    pg_catalog.pg_type t ON a.atttypid = t.oid
WHERE 
    c.relkind IN ('r', 'v', 'm')
    AND a.attnum > 0 
    AND NOT a.attisdropped
    AND n.nspname NOT LIKE 'pg\\_%'
    AND n.nspname != 'information_schema'
ORDER BY 
    attnum;

Casting these 2 lines seems to fix the issue:

    a.attidentity::text AS identity_type,
    a.attgenerated::text AS generated_type

It seems similar to this issue: https://github.com/drizzle-team/drizzle-kit-mirror/issues/542

Thank you for your help 😊

Expected behavior

No response

Environment & setup

No response

MaximeVivier commented 4 weeks ago

I'm getting the same kind of error on my project. Hoping someone has an answer to this issue. 🀞 Thanks !!!

thehamsti commented 4 weeks ago

Also getting this same error on same versions as OP.

eshrager commented 4 weeks ago

same error all of a sudden. it was fixed a couple of months back but now broken again

larryonward commented 3 weeks ago

here's the patch while we're waiting for proper fix

sed -i '' 's/const proxy = async (params) => {/const proxy = async (params) => {\n  params.sql = params.sql.replace(\/a.attidentity AS identity_type\/i, '\''a.attidentity::text AS identity_type'\''); \n  params.sql = params.sql.replace(\/a.attgenerated AS generated_type\/i, '\''a.attgenerated::text AS generated_type'\'');/g' node_modules/drizzle-kit/bin.cjs
peterhanania commented 3 weeks ago

+1

Getting the error all of a sudden

valentinbeggi commented 3 weeks ago

These are different issues but i'm linking them here because they are related to type casting in drizzle-studio. They make relation visualization / drizzle query editor unusable in the studio.

https://github.com/drizzle-team/drizzle-orm/issues/2583 https://github.com/drizzle-team/drizzle-orm/issues/3409

Would really be awesome if these were fixed πŸ™

NikolaRusakov commented 3 weeks ago

@larryonward, thank you for the sed expression.

I made a patch file out of your sed exp for pnpm patch / npx patch-package. This applies to the latest drizzle-kit version 0.28.0

https://gist.github.com/NikolaRusakov/88858b5b6a4e1585ca772f40f4e4d559#file-drizzle-kit-0-28-0-patch

rmarscher commented 1 day ago

The patches were working for me but they suddenly stopped today. 😒