Skip to content

SQLite getting-started tutorial uses LastInsertId() on RETURNING * result #4468

@yiereh

Description

@yiereh

Description

The SQLite getting-started tutorial appears to have an inconsistency between the SQL query and the Go usage example.

Page:

https://docs.sqlc.dev/en/latest/tutorials/getting-started-sqlite.html

The tutorial defines CreateAuthor as:

-- name: CreateAuthor :one
INSERT INTO authors (                                     
  name, bio
) VALUES (           
  ?, ?
)                                                                                                                      
RETURNING *;

With :one and RETURNING *, sqlc generates a method returning an Author:

                                                                                                                         
  func (q *Queries) CreateAuthor(ctx context.Context, arg CreateAuthorParams) (Author, error)

However, the later Go example uses the returned value as if it were a sql.Result:

  result, err := queries.CreateAuthor(ctx, tutorial.CreateAuthorParams{                                                  
      Name: "Brian Kernighan",                              
      Bio:  sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid:
      true},
  })

  insertedAuthorID, err := result.LastInsertId()

This does not compile because result is an Author, not a sql.Result.

Expected behavior

The tutorial should use the returned Author directly, for example:

  createdAuthor, err := queries.CreateAuthor(ctx, tutorial.CreateAuthorParams{
      Name: "Brian Kernighan",
      Bio:  sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid:
      true},
  })
  if err != nil {
      return err
  }
  log.Println(createdAuthor.ID)

Then the later fetch could use:

  fetchedAuthor, err := queries.GetAuthor(ctx, createdAuthor.ID)

Actual behavior

The tutorial currently calls:

  result.LastInsertId()

on the value returned by CreateAuthor, but that value is generated as an Author.

Version

I reproduced this with sqlc v1.31.1, which is also the version shown in generated files from the current tutorial.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions