Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: goctl pg gen will extract all fields when the same table name exists in different schemas (#3496) #3517

Merged
merged 1 commit into from
Sep 4, 2023

Conversation

POABOB
Copy link
Contributor

@POABOB POABOB commented Aug 23, 2023

Create two same tables in different schemas would cause error.
Because the origin sql would find all the tables, then left join these tables to the specific schema.
It didn't filter the specific schema when it use left join!
Like this:

select t.num,t.field,t.type,t.not_null,t.comment, c.column_default, identity_increment
from (
         SELECT a.attnum AS num,
                c.relname,
                a.attname     AS field,
                t.typname     AS type,
                a.atttypmod   AS lengthvar,
                a.attnotnull  AS not_null,
                b.description AS comment
         FROM pg_class c,
              pg_attribute a
                  LEFT OUTER JOIN pg_description b ON a.attrelid = b.objoid AND a.attnum = b.objsubid,
              pg_type t
         WHERE c.relname = $1
           and a.attnum > 0
           and a.attrelid = c.oid
           and a.atttypid = t.oid
 		 GROUP BY a.attnum, c.relname, a.attname, t.typname, a.atttypmod, a.attnotnull, b.description
         ORDER BY a.attnum) AS t
         left join information_schema.columns AS c on t.relname = c.table_name 
		and t.field = c.column_name and c.table_schema = $2
截圖 2023-08-24 上午12 01 37

This is a fixed code privoded by #3496

select t.num,t.field,t.type,t.not_null,t.comment, c.column_default, identity_increment
from (
         SELECT a.attnum AS num,
                c.relname,
                a.attname     AS field,
                t.typname     AS type,
                a.atttypmod   AS lengthvar,
                a.attnotnull  AS not_null,
                b.description AS comment,
                (c.relnamespace::regnamespace)::varchar AS schema_name
         FROM pg_class c,
              pg_attribute a
                  LEFT OUTER JOIN pg_description b ON a.attrelid = b.objoid AND a.attnum = b.objsubid,
              pg_type t
         WHERE c.relname = $1
           and a.attnum > 0
           and a.attrelid = c.oid
           and a.atttypid = t.oid
 		 GROUP BY a.attnum, c.relname, a.attname, t.typname, a.atttypmod, a.attnotnull, b.description, c.relnamespace::regnamespace
         ORDER BY a.attnum) AS t
         left join information_schema.columns AS c on t.relname = c.table_name and t.schema_name = c.table_schema
		and t.field = c.column_name
		where c.table_schema = $2

And It will work like this:
截圖 2023-08-23 下午11 58 26

@kesonan kesonan self-requested a review September 4, 2023 02:06
Copy link
Collaborator

@kesonan kesonan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@codecov
Copy link

codecov bot commented Sep 4, 2023

Codecov Report

Merging #3517 (0d124db) into master (c4ef9ce) will decrease coverage by 0.05%.
The diff coverage is n/a.

❗ Current head 0d124db differs from pull request most recent head 3b7a194. Consider uploading reports for the commit 3b7a194 to get more accurate results

see 3 files with indirect coverage changes

@kevwan kevwan merged commit 33f6d7e into zeromicro:master Sep 4, 2023
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants