default:

“default:” 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 “default:” ‘sqac:’ tag described in the Sqac Tag Overview section of this document.

Use the “default:” tag to explicitly declare default column values in the database table schema. The default values will be used only if no value has been provided for a defaulted column during record creation. A field with a “default:” tag will be defaulted at the database level if it contains a null value, or the go zero-value corresponding to the field’s go data type.

“default:<value>” example

The following example illustrates the use of ‘sqac:’ tag “default:<value>” to declare a column’s default value in the database table schema. Care should be taken to supply default values that correspond to the go data type of the the field/column.

  // Declare a struct to be used as the source for table creation.
  // sample default value assignments are shown
  type Depot struct {
    ...
    Region     string    `db:"region" sqac:"default:AB"`
    Efficiency float64   `db:"efficiency" sqac:"default:99.999"`
    Active     bool      `db:"active" sqac:"default:true"`
    ...
    ...
  }

time.Time “default:now()” function

Most database systems offer a selection of functions to provide default time and/or timestamp values. Sqac uses UTC timestamps as discussed in section X.Y.Z for fields declared as time.Time or *time.Time. “default:now()” will instruct the database to use its equivalent now() function to provide a default value for the field at time of record insertion. The defaulted value will be created as a UTC timestamp.

  // Declare a struct to be used as the source for table creation.
  // field will be defaulted to current timestamp at time of  record insertion
  type Depot struct {
    ...
    CreateDate time.Time `db:"create_date" sqac:"default:now();"`
    ...
    ...
  }

time.Time “default:eot()” function

Most database systems offer a selection of functions to provide default date-time and/or timestamp values. Sqac uses UTC timestamps as discussed in section X.Y.Z for fields declared as time.Time or *time.Time. “default:eot()” will set a default value for the field based on the maximum UTC timestamp supported by the database. Typically this will be along the lines of ‘9999 12 31 235959.999 UTC’, but some databases (MySQL/MariaDB) have lower maximum timestamp values.

  // Declare a struct to be used as the source for table creation.
  // field will be defaulted to current timestamp at time of  record insertion
  type Depot struct {
    ...
    ExpiryDate time.Time `db:"expiry_date" sqac:"default:eot();"`
    ...
    ...
  }