Gorm: string length

Created on 29 May 2015  路  4Comments  路  Source: go-gorm/gorm

How can i set mysql column to be longtext since struct type sting is 255 varchar?

Most helpful comment

use tags, e.g.:

type MyStruct struct {
   ...
    LongField string `sql:"type:text;"`
   ...
}

All 4 comments

use tags, e.g.:

type MyStruct struct {
   ...
    LongField string `sql:"type:text;"`
   ...
}

You could change the string length with sql:"size:999999" also

Hi Jay,

LongField string sql:"type:text;"

is not working.

When I do

dbmap.AddTableWithName(Post{}, "posts").SetKeys(true, "Id")

I am still getting 255 varchar.

type Post struct {
    Id      int64 `db:"post_id"`
    Created int64
    Title   string `form:"Title"`
    Body    string `sql:"type:text;"`

}
func initDb() *gorp.DbMap {
    // connect to db using standard Go database/sql API
    // use whatever database/sql driver you wish

    //db, err := sql.Open("sqlite3", "/tmp/post_db.bin")
    db, err := sql.Open("mysql", "user:pass@/martini")
    checkErr(err, "sql.Open failed")
    // construct a gorp DbMap
    dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
    // add a table, setting the table name to 'posts' and
    // specifying that the Id property is an auto incrementing PK
    dbmap.AddTableWithName(Post{}, "posts").SetKeys(true, "Id")
    // create the table. in a production system you'd generally
    // use a migration tool, or create the tables via scripts
    err = dbmap.CreateTablesIfNotExists()
    checkErr(err, "Create tables failed")
    return dbmap
}

@goors Work for my at least for MySQL by using

`sql:"type:text"`
Was this page helpful?
0 / 5 - 0 ratings

Related issues

fieryorc picture fieryorc  路  3Comments

pjebs picture pjebs  路  3Comments

izouxv picture izouxv  路  3Comments

rfyiamcool picture rfyiamcool  路  3Comments

satb picture satb  路  3Comments