gnormal / gnorm

A database-first code generator for any language
https://gnorm.org
Other
485 stars 40 forks source link

support reading table comments when two tables in different schemas h… #145

Open saward opened 2 years ago

saward commented 2 years ago

When you have two schemas with the same table name in them, you get the following error:

Error: error querying table comments: pq: more than one row returned by a subquery used as an expression

This change fixes that so that the query only fetches comments for the table that belongs to the schema that's being queried.

There might be other places in gnorm where it makes a similar assumption that table names will be unique across schemas, but for my project this change fixed my error and produces the same output as it did before I added the schema with table name matching the name from the other schema.

saward commented 1 year ago

Hi @natefinch, could you possibly review this and merge please?

marcobeierer commented 1 year ago

Hi @saward

I ran into the same issue today with three schema where tables with the same name existed in two schema.

I tried your PR, but it didn't solve the problem, had a closer look and found a solution. Shouldn't the subquery look like:

SELECT pg_catalog.col_description(c.oid, cols.ordinal_position::int)
FROM pg_catalog.pg_class c
    JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = cols.table_name
    AND n.nspname = cols.table_schema

instead of:

SELECT pg_catalog.col_description(c.oid, cols.ordinal_position::int)
FROM pg_catalog.pg_class c
    JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = cols.table_name
    AND n.nspname IN (%s)