Skip to content

Support PostgreSQL CREATE TABLE LIKE #481

Closed

Description

@mmcloughlin

The following create table statement

CREATE TABLE changes (
    ranked INT NOT NULL
);

CREATE TABLE changes_ranked (
    LIKE changes INCLUDING ALL,
    rank_by_effect_size INT NOT NULL,
    rank_by_abs_percent_change INT NOT NULL
);


-- name: AllRanked :many
SELECT * FROM changes_ranked;

produces model

type ChangesRanked struct {
       RankByEffectSize       int32
       RankByAbsPercentChange int32
}

Columns from the changes table are ignored.

(Aside: I encountered this while trying to work around #159. The changes_ranked table is an attempt to materialize what would have been a subquery.)

Activity

  1. added
    enhancementNew feature or request
    and removed
    bugSomething isn't working
    on Aug 28, 2021
  2. kyleconroy commented on Aug 28, 2021

    @kyleconroy
    Collaborator

    From the PostgreSQL docs:

    The LIKE clause specifies a table from which the new table automatically copies all column names, their data types, and their not-null constraints.

    Unlike INHERITS, the new table and original table are completely decoupled after creation is complete. Changes to the original table will not be applied to the new table, and it is not possible to include data of the new table in scans of the original table.

    Also unlike INHERITS, columns and constraints copied by LIKE are not merged with similarly named columns and constraints. If the same name is specified explicitly or in another LIKE clause, an error is signaled.

    The optional like_option clauses specify which additional properties of the original table to copy. Specifying INCLUDING copies the property, specifying EXCLUDING omits the property. EXCLUDING is the default. If multiple specifications are made for the same kind of object, the last one is used. The available options are:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions