Column definition
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' comment('NickName')"`
}
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 |
pk | If column is Primary Key |
support over 30 kinds of column types, details in Column Types | column type |
autoincr | If autoincrement column |
[not ]null | notnull | if 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. |
extends | use 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 |
created | This field will be filled in current time on insert |
updated | This field will be filled in current time on insert or update |
version | This field will be filled 1 on insert and autoincrement on update |
default 0 | default ‘name’ | column default value |
comment | set field comment (currently only supports mysql) |
Some default mapping rules:
- If field is name of
Id
and type ofint64
, xorm makes it as auto increment primary key. If another field, use struct tagxorm:"pk"
.
- If field is name of
- String is corresponding to varchar(255).
- 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.
- Support custom type as
- 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)
}
- 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.
- If one struct has a Conversion field, so we need set an implementation to the field before get data from database. We can implement
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 |