aldis-ameriks / pg-typegen

Generate TypeScript type definitions from Postgres database
MIT License
7 stars 1 forks source link

Relationship Does Not Exist Issue #37

Closed foopis23 closed 2 years ago

foopis23 commented 2 years ago

So I used this package on a previous project and it worked perfectly fine with no issue. Then, when I started a new project, I created a new database on the same version and even same DBMS as the first database, but when I tried to generated types I got this error here.

PostgresError: relation "insert_table_name_here" does not exist

I'm not a database expert, I can do basic queries, joins, and all of the intro to database stuff, but after fiddling with the query to getTableDefinitions for awhile I realized that if you just remove the parts of the select that use ::regclass the query doesn't throw the error, and from what I can tell my types still generated normally?

I still wonder if this is just a config issue on the database side of things, but I'm pretty sure everything is the exact same as the first project when it worked.

I would provide extra info about my database, but I don't exactly know what its helpful and what is not, so will provide if asked. I would really appreciate help resolving this.

Finally, if anyone run into this, and is wondering exactly what I did to get it to run. I changed node_modules/pg-typegen/src/postgres.js so that the getTableDefiniations functions looks like this

function getTableDefinitions (sql, schema) {
  return sql`
      SELECT c.table_name AS name,
             t.table_type = 'VIEW' AS "isView",
             jsonb_agg(
                     DISTINCT jsonb_build_object(
                     'name', column_name,
                     'type', udt_name,
                     'defaultValue', column_default,
                     'hasDefault',
                     column_default IS NOT NULL OR (is_identity = 'YES' AND identity_generation = 'ALWAYS'),
                     'isNullable', is_nullable::boolean,
                     'indices', (
                         array(
                                 SELECT jsonb_build_object(
                                                'name', i.relname,
                                                'isPrimaryKey', (
                                                    SELECT constraint_type
                                                    FROM information_schema.table_constraints tc
                                                    WHERE tc.constraint_name = i.relname
                                                      AND tc.constraint_type = 'PRIMARY KEY'
                                                ) IS NOT NULL
                                            )
                                 FROM pg_class t1,
                                      pg_class i,
                                      pg_index ix,
                                      pg_attribute a
                                 WHERE i.oid = ix.indexrelid
                                   AND t1.oid = ix.indrelid
                                   AND t1.relkind = 'r'
                                   AND a.attrelid = t1.oid
                                   AND a.attnum = ANY (ix.indkey)
                                   AND t1.relname = c.table_name
                                   AND a.attname = c.column_name
                             ))
                 )) AS columns
      FROM information_schema.columns c
           INNER JOIN information_schema.tables t ON t.table_name = c.table_name
      WHERE c.table_schema = ${schema}
        AND t.table_schema = ${schema}
      GROUP BY c.table_name, t.table_type
  `
  }
aldis-ameriks commented 2 years ago

Hi, @foopis23. What was the database version you used when you encountered the error?

foopis23 commented 2 years ago

9.5.25

aldis-ameriks commented 2 years ago

@foopis23 I cannot guarantee that pg-typegen will work as expected with v9 and older versions of postgres. Postgres v9 is obsolete and no longer supported. I'd suggest upgrading to a newer postgres version. If you encounter the same error on v10 or newer version of postgres, reach out and I'll try to assist.