Skip to content

Commit c7f4217

Browse files
EliSauderCopilot
andcommitted
fix(postgresql): keep an inherited column's own NOT NULL past its domain's
A child that redeclared an inherited domain column NOT NULL kept the mark that the NOT NULL came from the domain alone, so ALTER DOMAIN ... DROP NOT NULL cleared the child's own constraint. The child also shared the parent's column, so merging a NOT NULL into it changed the parent. Copy inherited columns and drop the domain mark when any merged declaration is NOT NULL by its own constraint. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0824475 commit c7f4217

8 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎internal/endtoend/testdata/ddl_create_table_inherits/postgresql/pgx/v4/go/models.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/ddl_create_table_inherits/postgresql/pgx/v5/go/models.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/ddl_create_table_inherits/postgresql/stdlib/go/models.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/domain_types/postgresql/go/models.go‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/domain_types/postgresql/go/query.sql.go‎

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/domain_types/postgresql/query.sql‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ SELECT * FROM shelves;
66

77
-- name: BooksByRef :many
88
SELECT b.id, b.title, s.label FROM books b LEFT JOIN shelves s ON s.label = b.title WHERE b.ref = $1;
9+
10+
-- name: ListNotes :many
11+
SELECT n.body, p.body AS pinned FROM notes n, pinned_notes p;

‎internal/endtoend/testdata/domain_types/postgresql/schema.sql‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ CREATE TABLE shelves (
4343
derived_list derived_list
4444
);
4545

46+
-- A child's own NOT NULL outlasts the domain's.
47+
CREATE TABLE notes (body relaxed);
48+
CREATE TABLE pinned_notes (body relaxed NOT NULL) INHERITS (notes);
49+
4650
-- The domain's NOT NULL leaves with it, and outlasts the column's own.
4751
ALTER TABLE shelves ALTER COLUMN retyped TYPE text;
4852
ALTER TABLE shelves ALTER COLUMN strict DROP NOT NULL;

‎internal/sql/catalog/table.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,15 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
286286
tbl := Table{Rel: stmt.Name, Comment: stmt.Comment}
287287
coltype := make(map[string]ast.TypeName) // used to check for duplicate column names
288288
seen := make(map[string]bool) // used to check for duplicate column names
289+
explicit := make(map[string]bool) // merged columns NOT NULL by their own constraint
289290
for _, inheritTable := range stmt.Inherits {
290291
t, _, err := schema.getTable(inheritTable)
291292
if err != nil {
292293
return err
293294
}
294295
// check and ignore duplicate columns
295296
for _, col := range t.Columns {
297+
explicit[col.Name] = explicit[col.Name] || (col.IsNotNull && !col.domainNotNull)
296298
if notNull, ok := seen[col.Name]; ok {
297299
seen[col.Name] = notNull || col.IsNotNull
298300
if a, ok := coltype[col.Name]; ok {
@@ -305,7 +307,8 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
305307

306308
seen[col.Name] = col.IsNotNull
307309
coltype[col.Name] = col.Type
308-
tbl.Columns = append(tbl.Columns, col)
310+
newCol := *col
311+
tbl.Columns = append(tbl.Columns, &newCol)
309312
}
310313
}
311314

@@ -328,6 +331,7 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
328331
}
329332
if notNull, ok := seen[col.Colname]; ok {
330333
seen[col.Colname] = notNull || col.IsNotNull
334+
explicit[col.Colname] = explicit[col.Colname] || col.IsNotNull
331335
if a, ok := coltype[col.Colname]; ok {
332336
if !sameType(&a, col.TypeName) {
333337
return fmt.Errorf("column %q has a type conflict", col.Colname)
@@ -346,6 +350,9 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
346350
for i := range tbl.Columns {
347351
if notNull, ok := seen[tbl.Columns[i].Name]; ok {
348352
tbl.Columns[i].IsNotNull = notNull
353+
if explicit[tbl.Columns[i].Name] {
354+
tbl.Columns[i].domainNotNull = false
355+
}
349356
}
350357
}
351358

0 commit comments

Comments
 (0)