Olu Udeh
Olu Udeh

Reputation: 1061

How do I create a TEXT column with Go Gorm

I am trying to create a TEXT column using Gorm ORM but the column is still created as VARCHAR(225). Below is the struct I want to migrate to a table.

type TextDump struct {
  *gorm.Model
  Title string `gorm:"varchar(50)" json:"title" binding:"required"`
  Text string `gorm:"text" json:"text" binding:"required"`
  Count int `json:"count"`
  ChannelID int `json:"channel_id" binding:"required"`
}

The text column is created as VARCHAR instead of TEXT.

Upvotes: 8

Views: 15119

Answers (4)

MD. Rejaul Hasan
MD. Rejaul Hasan

Reputation: 176

Title string `gorm:"type:varchar(50)"`

Upvotes: 1

Ömer Faruk PARLAR
Ömer Faruk PARLAR

Reputation: 171

type TextDump struct {
  *gorm.Model
  Title string `gorm:"size:50" json:"title" binding:"required"`
  Text string `gorm:"text" json:"text" binding:"required"`
  Count int `json:"count"`
  ChannelID int `json:"channel_id" binding:"required"`
}

Upvotes: -1

anon
anon

Reputation:

Reposting @Narro's comment here for better discoverability (I too had to check the comments).

You should use the tag name type before the column type:

type TextDump struct {
  *gorm.Model
  Text string `gorm:"type:text"`
  // ...
}

Reference: Gorm Field Tags

Upvotes: 8

iamcmnut
iamcmnut

Reputation: 479

Just create with a blank tag.

type Post struct {
  gorm.Model
  Content string ``
}

P.S. I'm using Gorm v1.20.12 with Postgres 11.8

Upvotes: 0

Related Questions