Skip to content

fix(contrib/drivers/pgsql): ensure fields queries are scoped to the specified schema#4491

Closed
qinyuguang wants to merge 1 commit intogogf:masterfrom
qinyuguang:fix/pgsql_table_fields
Closed

fix(contrib/drivers/pgsql): ensure fields queries are scoped to the specified schema#4491
qinyuguang wants to merge 1 commit intogogf:masterfrom
qinyuguang:fix/pgsql_table_fields

Conversation

@qinyuguang
Copy link
Contributor

This PR addresses an issue where database metadata queries, specifically those retrieving table and column information using PostgreSQL catalog views (pg_attribute, pg_class, etc.), were not correctly scoped by schema.

Problem:
When multiple schemas contain tables with the same name (e.g., schema1.some_table and public.some_table), the previous query would return metadata for all tables, leading to inaccurate or duplicate results.

Solution:
The query logic has been updated to explicitly join with pg_namespace and filter results based on the schema name (nspname).

  1. Joined pg_class with pg_namespace via c.relnamespace = n.oid.
  2. Added a WHERE clause condition: n.nspname = 'public' (or the target schema name).
  3. Ensured the join to information_schema.columns is also filtered by ic.table_schema = n.nspname for accurate data retrieval (e.g., default_value, length).

This ensures that the metadata query correctly retrieves information only for the table within the intended schema.

@qinyuguang
Copy link
Contributor Author

This bug has a direct impact on our database generation tools, specifically when using gf gen dao.

When gf gen dao is executed, it incorrectly merges the column definitions from identically named tables across multiple schemas into a single struct. This results in the generated Go struct containing combined, non-existent, or duplicated field names.

Consequently, when this generated dao is used for gdb Scan operations, the SQL query attempts to select fields that are not present in the intended single table, leading to an immediate SQL error (e.g., "column does not exist") or incorrect query results.

@gqcn
Copy link
Member

gqcn commented Oct 22, 2025

@qinyuguang Hello, the CI failed, you might also update the source codes of the CLI.

@qinyuguang qinyuguang force-pushed the fix/pgsql_table_fields branch from 5b4796c to 99c75f8 Compare October 22, 2025 15:32
@qinyuguang
Copy link
Contributor Author

I see. The "schema" in the parameters is not the actual schema in PostgreSQL. I'll replace it with "namespace" to make the distinction.
The naming of the variables is a bit confusing.

@hailaz
Copy link
Contributor

hailaz commented Oct 23, 2025

和这个pr有没有关联?#4375

@qinyuguang
Copy link
Contributor Author

和这个pr有没有关联?#4375

对是这个问题,用regclass很方便,我关掉这个PR

@qinyuguang qinyuguang closed this Oct 23, 2025
@qinyuguang qinyuguang deleted the fix/pgsql_table_fields branch October 23, 2025 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants