Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/endtoend/testdata/ddl_create_table_like/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/sqlc-dev/sqlc/issues/481

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: AllRanked :many
SELECT * FROM changes_ranked;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "pgx/v5"
32 changes: 14 additions & 18 deletions internal/sql/catalog/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,6 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
}
}

if stmt.ReferTable != nil && len(stmt.Cols) != 0 {
return errors.New("create table node cannot have both a ReferTable and Cols")
}

if stmt.ReferTable != nil {
_, original, err := c.getTable(stmt.ReferTable)
if err != nil {
Expand All @@ -302,23 +298,23 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
newCol := *col // make a copy, so changes to the ReferTable don't propagate
tbl.Columns = append(tbl.Columns, &newCol)
}
} else {
for _, col := range stmt.Cols {
if notNull, ok := seen[col.Colname]; ok {
seen[col.Colname] = notNull || col.IsNotNull
if a, ok := coltype[col.Colname]; ok {
if !sameType(&a, col.TypeName) {
return fmt.Errorf("column %q has a type conflict", col.Colname)
}
}

for _, col := range stmt.Cols {
if notNull, ok := seen[col.Colname]; ok {
seen[col.Colname] = notNull || col.IsNotNull
if a, ok := coltype[col.Colname]; ok {
if !sameType(&a, col.TypeName) {
return fmt.Errorf("column %q has a type conflict", col.Colname)
}
continue
}
tc, err := c.defineColumn(stmt.Name, col)
if err != nil {
return err
}
tbl.Columns = append(tbl.Columns, tc)
continue
}
tc, err := c.defineColumn(stmt.Name, col)
if err != nil {
return err
}
tbl.Columns = append(tbl.Columns, tc)
}

// If one of the merged columns was not null, mark the column as not null
Expand Down