Usage
- db - column name in db
package main
type _ struct {
ID int `db:"id"`
}pk
If set column is primary key
Example
package main
type _ struct {
ID int `db:"id" pk:"true"`
}nullable
If set then column is nullable(without NOT NULL)
Example
package main
type _ struct {
Code *int `db:"code" nullable:"true"`
}length
Set column length(useful for varchar)
Example
package main
type _ struct {
Name string `db:"name" length:"64"`
}uniq
If set then column is unique(can be placed to many properties - grouped, on single column can be many uniq’s, separated by ,)
Example
package main
// CREATE UNIQUE INDEX uniq_name ON example_table (name);
type _ struct {
Name string `db:"name" uniq:"uniq_name"`
}
// CREATE UNIQUE INDEX uniq_name_parted ON example_table (name_part1, name2);
type _ struct {
NamePart1 string `db:"name_part1" uniq:"uniq_name_parted"`
NamePart2 string `db:"name2" uniq:"uniq_name_parted"`
}uniq_cond
Works in pair with uniq, uniq_cond is a condition for uniq, can be placed on any column(of grouped uniq’s, do not set to every column of grouped uniq’s, can be multiple, separated by ,)
Example
package main
// CREATE UNIQUE INDEX uniq_name_parted ON example_table (name1, name2) WHERE is_selected = true;
type _ struct {
Checked bool `db:"is_checked"`
NamePart1 string `db:"name1" uniq:"uniq_name_parted" uniq_cond:"uniq_name_parted:(is_checked = true)"`
NamePart2 string `db:"name2" uniq:"uniq_name_parted"`
}index
If set then column is index(logic same as uniq)
Example
package main
// CREATE INDEX idx_name ON example_table (name);
type _ struct {
Name string `db:"name" index:"idx_name"`
}
// CREATE INDEX idx_name_parted ON example_table (name_part1, name2);
type _ struct {
NamePart1 string `db:"name_part1" index:"idx_name_parted"`
NamePart2 string `db:"name2" index:"idx_name_parted"`
}index_cond
Works in pair with index(logic same as uniq_cond)
Example
package main
// CREATE INDEX idx_name_parted ON example_table (name1, name2) WHERE is_selected = true;
type _ struct {
Checked bool `db:"is_checked"`
NamePart1 string `db:"name1" index:"idx_name_parted" index_cond:"idx_name_parted:(is_checked = true)"`
NamePart2 string `db:"name2" index:"idx_name_parted"`
}- default - if set then column has default value
package main
type _ struct {
Name string `db:"name" default:"123"`
}type
If set then column has type(manually set, without struct property type checking)
Example
package main
type ExampleType int
type _ struct {
Type ExampleType `db:"type" type:"int"`
}Types
This section outlines the supported database column types, their conditions, and aliases.
| Type | Condition | Aliases |
|---|---|---|
text | No conditions | None |
varchar | No conditions | None |
json | No conditions | None |
jsonb | No conditions | None |
integer | No conditions | None |
bigint | No conditions | None |
decimal | Requires scale and precision tags | None |
float | No conditions; defaults to float64 | None |
smallfloat | No conditions; defaults to float32 | None |
Decimal examples
type Example struct {
col_float64 float64 `db:"f4" type:"decimal" precision:"20" scale:"10"`
col_float_32 float32 `db:"f3" type:"decimal" precision:"10" scale:"2"`
}Last updated on