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: resolve duplicate fields generated when inheriting multiple tables #2089

Merged
merged 6 commits into from
Jun 7, 2023

Conversation

ethan-xiao
Copy link
Contributor

@ethan-xiao ethan-xiao commented Feb 16, 2023

For example, I created tables following:
create table example_a {
id integer not null,
name varchar(32),
};
create table example_b {} inherits (example_a);
create table example_c {} inherits (example_a, example_b);

and then sqlc generated golang struct for table example_c:
type ExampleC struct {
ID int32
Name string
ID int32
Name string
}

So I fixed this in this PR.

@ethan-xiao ethan-xiao reopened this Mar 29, 2023
Copy link
Collaborator

@kyleconroy kyleconroy left a comment

Choose a reason for hiding this comment

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

Could you please add a new end-to-end test for this behavior? It's also helpful to see what the previous behavior looked like.

@kyleconroy
Copy link
Collaborator

A table can inherit from more than one parent table, in which case it has the union of the columns defined by the parent tables. Any columns declared in the child table's definition are added to these. If the same column name appears in multiple parent tables, or in both a parent table and the child's definition, then these columns are “merged” so that there is only one such column in the child table. To be merged, columns must have the same data types, else an error is raised. Inheritable check constraints and not-null constraints are merged in a similar fashion. Thus, for example, a merged column will be marked not-null if any one of the column definitions it came from is marked not-null. Check constraints are merged if they have the same name, and the merge will fail if their conditions are different.

https://www.postgresql.org/docs/current/ddl-inherit.html

@kyleconroy kyleconroy merged commit 0c340f2 into sqlc-dev:main Jun 7, 2023
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.

2 participants