constraint:

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

The “constraint:” tag is presently only used to declare unique constraints on single columns. It would be relatively simple to add support for any desired database constraint however.

“constraint:unique” example

The following example illustrates the use of ‘sqac:’ tag “constraint:” to declare a unique column constraint. Creation of a unique constraint on a single column results in an underlying db constraint, but does not necessarily create an index on the constrained column. Check the target db documentation for details.

If a column has been declared with a single-column index, the “constraint:unique” tag is redundant. If a column has been declared with unique constraint, it may still be included in a compound “index:” declaration.

  // Declare a struct to be used as the source for table creation.
  type Depot struct {
    ...
    AccountNum int       `db:"account_num" sqac:"nullable:false;constraint:unique"`    // unique constraint
    ...
    ...
  }