diff --git a/internal/endtoend/testdata/primary_key_later/go/db.go b/internal/endtoend/testdata/primary_key_later/go/db.go new file mode 100644 index 0000000000..ae95420bdc --- /dev/null +++ b/internal/endtoend/testdata/primary_key_later/go/db.go @@ -0,0 +1,29 @@ +// Code generated by sqlc. DO NOT EDIT. + +package primary_key_later + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/primary_key_later/go/models.go b/internal/endtoend/testdata/primary_key_later/go/models.go new file mode 100644 index 0000000000..6f99a23a5b --- /dev/null +++ b/internal/endtoend/testdata/primary_key_later/go/models.go @@ -0,0 +1,13 @@ +// Code generated by sqlc. DO NOT EDIT. + +package primary_key_later + +import ( + "database/sql" +) + +type Author struct { + ID int64 + Name string + Bio sql.NullString +} diff --git a/internal/endtoend/testdata/primary_key_later/go/queries.sql.go b/internal/endtoend/testdata/primary_key_later/go/queries.sql.go new file mode 100644 index 0000000000..6a559f8873 --- /dev/null +++ b/internal/endtoend/testdata/primary_key_later/go/queries.sql.go @@ -0,0 +1,25 @@ +// Code generated by sqlc. DO NOT EDIT. +// source: queries.sql + +package primary_key_later + +import ( + "context" +) + +const getAuthor = `-- name: GetAuthor :one +SELECT + id, name, bio +FROM + authors +WHERE + id = $1 +LIMIT 1 +` + +func (q *Queries) GetAuthor(ctx context.Context, id int64) (Author, error) { + row := q.db.QueryRowContext(ctx, getAuthor, id) + var i Author + err := row.Scan(&i.ID, &i.Name, &i.Bio) + return i, err +} diff --git a/internal/endtoend/testdata/primary_key_later/queries.sql b/internal/endtoend/testdata/primary_key_later/queries.sql new file mode 100644 index 0000000000..158a7c66bc --- /dev/null +++ b/internal/endtoend/testdata/primary_key_later/queries.sql @@ -0,0 +1,9 @@ +-- name: GetAuthor :one +SELECT + * +FROM + authors +WHERE + id = $1 +LIMIT 1; + diff --git a/internal/endtoend/testdata/primary_key_later/schema.sql b/internal/endtoend/testdata/primary_key_later/schema.sql new file mode 100644 index 0000000000..652d4f1f9d --- /dev/null +++ b/internal/endtoend/testdata/primary_key_later/schema.sql @@ -0,0 +1,7 @@ +CREATE TABLE authors ( + id bigserial, + name text NOT NULL, + bio text, + PRIMARY KEY (id) +); + diff --git a/internal/endtoend/testdata/primary_key_later/sqlc.json b/internal/endtoend/testdata/primary_key_later/sqlc.json new file mode 100644 index 0000000000..498f89cb24 --- /dev/null +++ b/internal/endtoend/testdata/primary_key_later/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "engine": "postgresql", + "path": "go", + "name": "primary_key_later", + "schema": "schema.sql", + "queries": "queries.sql" + } + ] +} diff --git a/internal/engine/postgresql/parse.go b/internal/engine/postgresql/parse.go index 4d414299a1..1e052e5652 100644 --- a/internal/engine/postgresql/parse.go +++ b/internal/engine/postgresql/parse.go @@ -399,6 +399,17 @@ func translate(node nodes.Node) (ast.Node, error) { Name: name, IfNotExists: n.IfNotExists, } + primaryKey := make(map[string]bool) + for _, elt := range n.TableElts.Items { + switch n := elt.(type) { + case nodes.Constraint: + if n.Contype == nodes.CONSTR_PRIMARY { + for _, item := range n.Keys.Items { + primaryKey[item.(nodes.String).Str] = true + } + } + } + } for _, elt := range n.TableElts.Items { switch n := elt.(type) { case nodes.ColumnDef: @@ -409,7 +420,7 @@ func translate(node nodes.Node) (ast.Node, error) { create.Cols = append(create.Cols, &ast.ColumnDef{ Colname: *n.Colname, TypeName: tn, - IsNotNull: isNotNull(n), + IsNotNull: isNotNull(n) || primaryKey[*n.Colname], IsArray: isArray(n.TypeName), }) }