为什么GORM不能生成外键?

0h4hbjxa  于 2023-06-27  发布在  Go
关注(0)|答案(3)|浏览(179)

我试图在Password表上创建一个外键,它必须指向User表中的id列。但当我尝试以下方法时,它不起作用。foreign key不会生成。它只是在password表中添加列名user_id

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表的外键?

yfjy0ee7

yfjy0ee71#

我认为你需要:

db.Model(&Password{}).AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT")

我把我的放在我的自动迁移语句之后,就像这样

db.AutoMigrate(&User{}, &Password{})
db.Model(&Password{}).AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT")

如果有帮助就告诉我。

j9per5c4

j9per5c42#

在我们的例子中,我们需要在列名/字段名方面具有完全的灵活性,因为我们正在迁移一个最初由SQLAlchemy创建的现有数据库。不使用Gorm期望的名称会导致不生成外键。
使用Gorm 2.0,由于某种原因仍然有v1标签(gorm.io/gorm v1.25.1),技巧是正确使用foreignkey(必须指向本地字段)和references(大多数指向外部表中的远程字段)注解完全任意的名称:

type Other struct {
    OtherId int32 `gorm:"primarykey; column:custom_col_other_id"`
}

type Main struct {
    MainId    int32 `gorm:"primarykey; column:custom_col_main_id"`
    FkOtherId int32 `gorm:"column:custom_col_fk_other_id"`
    OtherRef  Other `gorm:"foreignkey:FkOtherId;references:OtherId"`
}

...

dbClient.AutoMigrate(&Other{})
dbClient.AutoMigrate(&Main{})

这将产生正确的外键关系:

Table "public.others"
       Column        |  Type   | Collation | Nullable |                       Default                       
---------------------+---------+-----------+----------+-----------------------------------------------------
 custom_col_other_id | integer |           | not null | nextval('others_custom_col_other_id_seq'::regclass)
Indexes:
    "others_pkey" PRIMARY KEY, btree (custom_col_other_id)
Referenced by:
    TABLE "mains" CONSTRAINT "fk_mains_other_ref" FOREIGN KEY (custom_col_fk_other_id) REFERENCES others(custom_col_other_id)"

                                            Table "public.mains"
         Column         |  Type   | Collation | Nullable |                      Default                      
------------------------+---------+-----------+----------+---------------------------------------------------
 custom_col_main_id     | integer |           | not null | nextval('mains_custom_col_main_id_seq'::regclass)
 custom_col_fk_other_id | integer |           |          | 
Indexes:
    "mains_pkey" PRIMARY KEY, btree (custom_col_main_id)
Foreign-key constraints:
    "fk_mains_other_ref" FOREIGN KEY (custom_col_fk_other_id) REFERENCES others(custom_col_other_id)
oknwwptz

oknwwptz3#

试试这个。

type Password struct {
   gorm.Model
   Password string `gorm:"not null"`
   UserId int `gorm:"not null"`
   User User `gorm:"foreignkey:UserId;references:id"`
}

相关问题