Column definition

2.4.Column definition

Struct tag defines something for column as basic SQL concepts, such as :

  1. type User struct {
  2. Id int64
  3. Name string `xorm:"varchar(25) not null unique 'usr_name' comment('NickName')"`
  4. }

Data types are different in different DBMS. So xorm makes own data types definition to keep compatible. Details is in document Column Types.

The following table is field mapping rules, the keyword is not case sensitive except column name:

name or ‘name’Column Name, optional
pkIf column is Primary Key
support over 30 kinds of column types, details in Column Typescolumn type
autoincrIf autoincrement column
[not ]null | notnullif column could be blank
unique/unique(uniquename)column is Unique index; if add (uniquename), the column is used for combined unique index with the field that defining same uniquename.
index/index(indexname)column is index. if add (indexname), the column is used for combined index with the field that defining same indexname.
extendsuse for anonymous field, map the struct in anonymous field to database
-This field will not be mapping
->only write into database
<-only read from database
createdThis field will be filled in current time on insert
updatedThis field will be filled in current time on insert or update
versionThis field will be filled 1 on insert and autoincrement on update
default 0 | default ‘name’column default value
commentset field comment (currently only supports mysql)

Some default mapping rules:

    1. If field is name of Id and type of int64, xorm makes it as auto increment primary key. If another field, use struct tag xorm:"pk".
    1. String is corresponding to varchar(255).
    1. Support custom type as type MyString string,slice, map as field type. They are saving as Text column type and json-encode string. Support Blob column type with field type []byte or []uint8.
    1. You can implement Conversion interface to define your custom mapping rule between field and database data.
  1. type Conversion interface {
  2. FromDB([]byte) error
  3. ToDB() ([]byte, error)
  4. }
    1. If one struct has a Conversion field, so we need set an implementation to the field before get data from database. We can implement BeforeSet(name string, cell xorm.Cell) on struct to do this.
xormmysqlsqlite3postgresremark
BITBITINTEGERBIT
TINYINTTINYINTINTEGERSMALLINT
SMALLINTSMALLINTINTEGERSMALLINT
MEDIUMINTMEDIUMINTINTEGERINTEGER
INTINTINTEGERINTEGER
INTEGERINTEGERINTEGERINTEGER
BIGINTBIGINTINTEGERBIGINT
CHARCHARTEXTCHAR
VARCHARVARCHARTEXTVARCHAR
TINYTEXTTINYTEXTTEXTTEXT
TEXTTEXTTEXTTEXT
MEDIUMTEXTMEDIUMTEXTTEXTTEXT
LONGTEXTLONGTEXTTEXTTEXT
BINARYBINARYBLOBBYTEA
VARBINARYVARBINARYBLOBBYTEA
DATEDATENUMERICDATE
DATETIMEDATETIMENUMERICTIMESTAMP
TIMETIMENUMERICTIME
TIMESTAMPTIMESTAMPNUMERICTIMESTAMP
TIMESTAMPZTEXTTEXTTIMESTAMP with zonetimestamp with zone info
REALREALREALREAL
FLOATFLOATREALREAL
DOUBLEDOUBLEREALDOUBLE PRECISION
DECIMALDECIMALNUMERICDECIMAL
NUMERICNUMERICNUMERICNUMERIC
TINYBLOBTINYBLOBBLOBBYTEA
BLOBBLOBBLOBBYTEA
MEDIUMBLOBMEDIUMBLOBBLOBBYTEA
LONGBLOBLONGBLOBBLOBBYTEA
BYTEABLOBBLOBBYTEA
BOOLTINYINTINTEGERBOOLEAN
SERIALINTINTEGERSERIALauto increment
BIGSERIALBIGINTINTEGERBIGSERIALauto increment