我正在尝试在Password表上创建一个外键,该外键必须指向表id内的User列。但是当我尝试以下操作时,它不起作用。foreign key没有生成。user_id它只是在表中添加列名password。
package schema
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
type User struct {
gorm.Model
FirstName string `gorm:"not null"`
LastName string `gorm:"not null"`
Email string `gorm:"type:varchar(100);unique_index"`
IsActive bool `gorm:"not null"`
IsVerified bool `gorm:"not null"`
}
type Password struct {
gorm.Model
Password string `gorm:"not null"`
UserId int `gorm:"not null"`
User User `gorm:"foreignkey:UserId;association_foreignkey:id"`
}
func SyncDB(db *gorm.DB) {
db.AutoMigrate(&User{})
db.AutoMigrate(&Password{})
}
我究竟做错了什么?我怎样才能在Password表内创建一个指向User表的外键?
波斯汪
慕容708150
相关分类