gnormal / gnorm

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

Support for keyword table names in Postgres #117

Closed freb closed 4 years ago

freb commented 5 years ago

When attempting to use a table name that is also a keyword, you get an error when running gnorm gen, such as the following

Should be impossible: index "user_pkey" references unknown table "\"user\""

The issue is that all functions/queries to gather database metadata return the table name unquoted (queryForeignKeys, queryPrimaryKeys, queryColumnComments, queryTableComments), except for one (queryIndexes). When the quoted and non-quoted table names are compared here, they don't match, producing the error.

Since only one of the queries returns a quoted table name, I think the best approach is to simply return a non-quoted value from that query also. I don't think it makes sense to try to worry about storing or managing double quoted identifiers inside of Gnorm when it is probably better handled in templates anyway. The postgres-go templates prefix all table names with the schema anyway, which makes double quoting unnecessary.

My solution (seems like there should be a more straightforward change) was to replace the line with

trim(both '"' from i.indrelid::regclass::text) as table,