Skip to content

Commit 13b8ca8

Browse files
EliSauderCopilot
andcommitted
fix(postgresql): honor NOT NULL on a domain
A column typed with a domain declared NOT NULL was still nullable, so its Go field was a nullable wrapper for a value that can never be NULL. A column of such a domain is now NOT NULL, as PostgreSQL enforces it; an array of one may still be NULL, and the optional side of an outer join is still nullable. Fixes #3265 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent bcd8bb8 commit 13b8ca8

5 files changed

Lines changed: 18 additions & 8 deletions

File tree

‎internal/endtoend/testdata/codegen_json_domains_postgresql/gen/codegen.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
},
6363
{
6464
"name": "title",
65-
"not_null": false,
65+
"not_null": true,
6666
"is_array": false,
6767
"comment": "",
6868
"length": -1,
@@ -68862,7 +68862,7 @@
6886268862
},
6886368863
{
6886468864
"name": "title",
68865-
"not_null": false,
68865+
"not_null": true,
6886668866
"is_array": false,
6886768867
"comment": "",
6886868868
"length": -1,

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

Lines changed: 2 additions & 2 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: 4 additions & 3 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ SELECT id, title, price, ref FROM books WHERE price > $1;
55
SELECT * FROM shelves;
66

77
-- name: BooksByRef :many
8-
SELECT b.id, b.title FROM books b LEFT JOIN shelves s ON s.label = b.title WHERE b.ref = $1;
8+
SELECT b.id, b.title, s.label FROM books b LEFT JOIN shelves s ON s.label = b.title WHERE b.ref = $1;

‎internal/sql/catalog/table.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,15 @@ func (c *Catalog) defineColumn(table *ast.TableName, col *ast.ColumnDef) (*Colum
357357
Comment: col.Comment,
358358
Length: col.Length,
359359
}
360+
// A domain declared NOT NULL forbids NULL in every column of it. An
361+
// array of one may still be NULL itself.
362+
if !col.IsArray && col.TypeName != nil {
363+
if typ, _, err := c.getType(col.TypeName); err == nil {
364+
if d, ok := typ.(*Domain); ok && d.NotNull {
365+
tc.IsNotNull = true
366+
}
367+
}
368+
}
360369
if col.Vals != nil {
361370
typeName := ast.TypeName{
362371
Name: fmt.Sprintf("%s_%s", table.Name, col.Colname),

0 commit comments

Comments
 (0)