Skip to content

Conversation

@timwmillard
Copy link
Contributor

@timwmillard timwmillard commented Dec 13, 2021

Support for Postgres Inheritance.
https://www.postgresql.org/docs/current/ddl-inherit.html

Fixes #403

Add the columns from the parent tables to the child table.

This change also adds support for columns from partition tables.

@kyleconroy kyleconroy merged commit 22fa5f3 into sqlc-dev:main Dec 14, 2021
@kyleconroy
Copy link
Collaborator

kyleconroy commented Dec 14, 2021

@timwmillard How does inheritance with DDL statements that alter tables? The PostgreSQL docs seem to imply that all child tables should also be updated:

Commands that are used for data querying, data modification, or schema modification (e.g., SELECT, UPDATE, DELETE, most variants of ALTER TABLE, but not INSERT or ALTER TABLE ... RENAME) typically default to including child tables and support the ONLY notation to exclude them

@timwmillard
Copy link
Contributor Author

timwmillard commented Dec 14, 2021

@timwmillard How does inheritance with DDL statements that alter tables? The PostgreSQL docs seem to imply that all child tables should also be updated:

Commands that are used for data querying, data modification, or schema modification (e.g., SELECT, UPDATE, DELETE, most variants of ALTER TABLE, but not INSERT or ALTER TABLE ... RENAME) typically default to including child tables and support the ONLY notation to exclude them

@kyleconroy, I did look into this. When altering a table to add inheritance the child table needs to already have all the parent column on the table.

Example:

CREATE TABLE parent (
   name text
);

CREATE TABLE child (
   name text
   age text
);

ALTER TABLE child INHERIT parent;

In this example the child table already has the name column. If it didn't the ALTER statement would fail. Therefore the generated queries already have the correct columns.

@timwmillard
Copy link
Contributor Author

timwmillard commented Dec 14, 2021

Same as if you ALTER TABLE child NO INHERIT and remove the inheritance, the columns will remain the same, but it just won't have the internal inherit link.

@jlisthood
Copy link
Contributor

@timwmillard How does inheritance with DDL statements that alter tables? The PostgreSQL docs seem to imply that all child tables should also be updated:

Commands that are used for data querying, data modification, or schema modification (e.g., SELECT, UPDATE, DELETE, most variants of ALTER TABLE, but not INSERT or ALTER TABLE ... RENAME) typically default to including child tables and support the ONLY notation to exclude them

So are ALTER statements just not supported for inheritance then? I can alter base tables without seeing derived tables getting the new fields: https://play.sqlc.dev/p/0a50edacd24090cbb53d83ec6fe69b11ba109a0ab6a80ae2e33aec35a6b6bef3

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.

Postgres: Support inherited table schema

3 participants