index:

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

Use the “index:” tag to declare unique/non-unique indexes in database table schemas. Single column and compound indexes are supported.

“index:<unique | non-unique>” example

The following example illustrates the use of ‘sqac:’ tag “index:” to declare a single column unique and single column non-unique index on two table fields. Creation of a unique index on a single column results in an underlying db constraint. The unique constraint prevents the insertion of records when the indexed field contains a duplicate value.

  // Declare a struct to be used as the source for table creation.
  type Depot struct {
    ...
    Region     string    `db:"region" sqac:"default:AB;index:non-unique"`         // non-unique index
    AccountNum int       `db:"account_num" sqac:"nullable:false;index:unique"`    // unique index
    ...
    ...
  }

“index:” compound example

The following example illustrates the use of ‘sqac:’ tag “index:” to declare a multi-column non-unique index using two table fields. As of this writing, sqac does not support the creation of a unique compound index through the ‘sqac:’ tag model declarations.

Conventionally the compound index should be named as shown in the example below.

idx_<table_name>_<field_name1>_<field_name2>_<field_name*n*>

  // Declare a struct to be used as the source for table creation.
  type Depot struct {
    ...
    Country    string    `db:"country" sqac:"nullable:false;default:CA;index:idx_depot_country_province"`
    Province   string    `db:"province" sqac:"nullable:false;default:AB;index:idx_depot_country_province"`
    ...
    ...
  }