2.4.Column definition

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

type User struct {
    Id   int64
    Name string  `xorm:"varchar(25) not null unique 'usr_name'"`
}

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 Types](http://gobook.io/read/github.com/go-xorm/manual-en-US/chapter-02/1.mapping.html)column 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

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.
type Conversion interface {
    FromDB([]byte) error
    ToDB() ([]byte, error)
}
  • 5. 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. For example: testConversion
xorm mysql sqlite3 postgres remark
BIT BIT INTEGER BIT
TINYINT TINYINT INTEGER SMALLINT
SMALLINT SMALLINT INTEGER SMALLINT
MEDIUMINT MEDIUMINT INTEGER INTEGER
INT INT INTEGER INTEGER
INTEGER INTEGER INTEGER INTEGER
BIGINT BIGINT INTEGER BIGINT
CHAR CHAR TEXT CHAR
VARCHAR VARCHAR TEXT VARCHAR
TINYTEXT TINYTEXT TEXT TEXT
TEXT TEXT TEXT TEXT
MEDIUMTEXT MEDIUMTEXT TEXT TEXT
LONGTEXT LONGTEXT TEXT TEXT
BINARY BINARY BLOB BYTEA
VARBINARY VARBINARY BLOB BYTEA
DATE DATE NUMERIC DATE
DATETIME DATETIME NUMERIC TIMESTAMP
TIME TIME NUMERIC TIME
TIMESTAMP TIMESTAMP NUMERIC TIMESTAMP
TIMESTAMPZ TEXT TEXT TIMESTAMP with zone timestamp with zone info
REAL REAL REAL REAL
FLOAT FLOAT REAL REAL
DOUBLE DOUBLE REAL DOUBLE PRECISION
DECIMAL DECIMAL NUMERIC DECIMAL
NUMERIC NUMERIC NUMERIC NUMERIC
TINYBLOB TINYBLOB BLOB BYTEA
BLOB BLOB BLOB BYTEA
MEDIUMBLOB MEDIUMBLOB BLOB BYTEA
LONGBLOB LONGBLOB BLOB BYTEA
BYTEA BLOB BLOB BYTEA
BOOL TINYINT INTEGER BOOLEAN
SERIAL INT INTEGER SERIAL auto increment
BIGSERIAL BIGINT INTEGER BIGSERIAL auto increment