2

GORM Set size for the string field

 2 years ago
source link: https://kenanbek.github.io/go-gorm-varchar-size
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

GORM Set size for the string field

Published: Jun 8, 2020 / Last modified: Jun 8, 2020

In GORM package, you can specify field’s size using 2 options.

  1. Using gorm:size tag
  2. Using gorm:type tag

An example using gorm:size:

type Article struct {
	gorm.Model

	Source   Source `json:"source" gorm:"foreignkey:SourceID"`
	SourceID uint   `json:"sourde_ref"`
	Group    string `json:"group" gorm:"not null"`
	FromURL  string `json:"from_url"`
	URL      string `json:"url" gorm:"unique;not null"`

	Title           string `json:"title" gorm:"size:1000"`
	Description     string `json:"description" gorm:"size:65535"`
	Content         string `json:"content" gorm:"size:65535"`
	AuthorName      string
	AuthorEmail     string
	ImageURL        string
	ImageTitle      string
	Published       string
	PublishedParsed *time.Time
	Updated         string
	UpdatedParsed   *time.Time
	Categories      string
}

Another example using gorm:type:

type User struct {
  gorm.Model
  Name         string
  Age          sql.NullInt64
  Birthday     *time.Time
  Email        string  `gorm:"type:varchar(100);unique_index"`
}

Check this page for more details.

[ Tags ]

go gorm varchar string model golang

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK