Description

when i used User.Name for primary_key,call AutoMigrate() will got error,like this:

2020/09/03 15:05:30 /Users/mybook/go/src/demo/src/db/connect.go:30 Error 1170: BLOB/TEXT column 'user_refer' used in key specification without a key length
[0.417ms] [rows:0] CREATE TABLE `credit_cards` (`number` longtext,`user_refer` longtext,CONSTRAINT `fk_users_credit_cards` FOREIGN KEY (`user_refer`) REFERENCES `users`(`name`))
2020/09/03 15:05:30 Failed to auto migrate, but got error Error 1170: BLOB/TEXT column 'user_refer' used in key specification without a key length

my models :

type User struct {
    Name string `gorm:"primary_key"`
    CreditCards []CreditCard `gorm:"foreignKey:UserRefer"`
}

type CreditCard struct {
    Number    string
    UserRefer string
}

ENV

gorm.io/driver/mysql v1.0.1
gorm.io/gorm v1.20.0

go version go1.14.4 darwin/amd64

Comment From: github-actions[bot]

This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jinzhu

set size for field UserRefer, e.g:

type CreditCard struct {
    Number    string
    UserRefer string `gorm:"size:191"`
}

Comment From: diluga

set size for field UserRefer, e.g:

go type CreditCard struct { Number string UserRefer string `gorm:"size:191"` }

thx!

Comment From: justincletus

set size for field UserRefer, e.g:

go type CreditCard struct { Number string UserRefer string `gorm:"size:191"` }

@diluga Thanks this works for me too

Comment From: pomozoff

What is the difference with the length attribute?

Comment From: mafif21

go size:191

TYSM bro its work for me ✨