Skip to content

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
alfonsodev pushed a commit to ExponentiaTeam/sqlc that referenced this pull request Oct 13, 2025
…es (sqlc-dev#2089)

* fix: resolve duplicate fields generated when inheriting multiple tables

* chore: comments

* fix: resolve duplicate fields generated when inheriting multiple tables (sqlc-dev#1)
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