nullable:

“nullable:” Overview

Sqac annotated go structs are used to supply the sqac runtime with the information required to create and alter tables in the database. The following examples illustrate the use of the “nullable:” ‘sqac:’ tag described in the Sqac Tag Overview section of this document.

Unless specified, most databases will create columns as nullable by default. Use the “nullable:” tag to explicitly declare the data requirements of fields in the source go-struct and the nullability of the corresponding columns in the database.

“nullable:false” example

The following example illustrates the use of ‘sqac:’ tag “nullable:false” to declare a column as not null in the database. If no value can be provided for a column deemed to be not nullable, consider setting a default value at the database level via the “default:” ‘sqac:’ tag, or provide the go zero-value for the field.

  // Declare a struct to be used as the source for table creation.
  // db column "region" will be created as NOT NULL
  type Depot struct {
    ...
    Region     string    `db:"region" sqac:"nullable:false"`
    ...
    ...
  }

“nullable:true” example

The following example illustrates the use of ‘sqac:’ tag “nullable:true” to declare a column as nullable in the database. Care should be taken when deciding to create a column as nullable, as the consumer may not be expecting null values in the selected data. Note that reading a null value into a non-pointer field in the go-struct will result in an error. In the following example code the “Region” field has been declared as a pointer to a string (*string) rather than a string for this reason.

  // Declare a struct to be used as the source for table creation.
  // db column "region" will be nullable in the database
  type Depot struct {
    ...
    Region     *string    `db:"region" sqac:"nullable:true"`
    ...
    ...
  }