GORM v1.25.1,我在WorkerPosterJob型号上运行DB.AutoMigrate(),但在[error] unsupported data type: &[]上运行.Worker和Job struct 应该有many-to-many relation,而Poster和Job应该有one-to-many个关系.工作者和经验、工作者和偏好都应该是one-to-many的关系.请帮帮忙.

package model

type experience struct {
    gorm.Model
    Company  int    `json:"company"`
    JobTitle string `json:"jobTitle"`
    WorkerID uint
}

type preference struct {
    gorm.Model
    JobTitle string `json:"JobTitle"`
    MinPay   int    `json:"minPay"`
    MaxPay   int    `json:"maxPay"`
    WorkerID uint
}

type Worker struct {
    gorm.Model
    Username     string       `gorm:"uniqueIndex;not null" json:"username"`
        ...more fields
    Experience   []experience `json:"experience"`
    Preference   []preference `json:"preference"`
    AppliedJobs  []Job        `gorm:"many2many:worker_jobs;" json:"appliedJobs"`
}

type Poster struct {
    gorm.Model
    Name       string `gorm:"uniqueIndex;not null" json:"name"`
    Email      string `gorm:"uniqueIndex;not null" json:"email"`
    Phone      string `json:"phone"`
    JobsPosted []Job  `json:"jobsPosted"`
}

type Job struct {
    gorm.Model
    Title       string   `gorm:"uniqueIndex;not null" json:"title"`
        ...more fields
    PosterID uint `json:"posterID"`
}

推荐答案

通过以下代码,我能够实现您所需要的功能.

请注意,出于演示的目的,我简化了您的模型( struct ),只包含关联的相关字段.如果我遗漏了一些值得一提的东西,请随时询问,我会更新我的回答.

让我先分享代码,然后再详细解释.

package main

import (
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
)

type Worker struct {
    gorm.Model
    Username string `gorm:"uniqueIndex;not null" json:"username"`

    Posters []Poster `gorm:"many2many:workers_posters;joinForeignKey:postersId"`
}

type Poster struct {
    gorm.Model
    Name  string `gorm:"uniqueIndex;not null" json:"name"`
    Email string `gorm:"uniqueIndex;not null" json:"email"`
    Phone string `json:"phone"`

    Workers []Worker `gorm:"many2many:workers_posters;joinForeignKey:workersId"`
    Jobs    []Job
}

type Job struct {
    gorm.Model
    Title    string `gorm:"uniqueIndex;not null" json:"title"`
    PosterID uint   `json:"posterID"`
}

func main() {
    dsn := "host=localhost port=54322 user=postgres password=postgres dbname=postgres sslmode=disable"
    db, err := gorm.Open(postgres.Open(dsn))
    if err != nil {
        panic(err)
    }

    db.AutoMigrate(&Worker{}, &Poster{}, &Job{})
}

由于你的问题既涉及many2many关系,也涉及one2many关系,我将把我的答案分为两部分.

The many2many relationship

要实现这种关系,您必须在包含关联实体的切片旁边添加注释gorm:"many2many:workers_posters;joinForeignKey:workersId".受影响的字段包括:

  • Worker struct 中的Posters字段
  • Poster struct 中的Workers字段

显然,您必须根据要设置的字段更改joinForeignKey的值.

The one2many relationship

这种关系甚至更简单,因为您不必指定必须在many2many关联中创建的连接表(例如,上面创建的workers_posters表).在这里,您只需进行以下两个更改:

  1. Poster struct 中添加Jobs []Job字段
  2. Job struct 中添加PosterID uint字段

多亏了这一点,如果您运行Automigrate方法,您将在数据库中看到正确设置了所有外键的表.

让我知道,谢谢!

Go相关问答推荐

如何将泛型函数作为参数传递给golang中的另一个函数?

Zitadel示例Go Webapp加密密钥

golang有int32溢出吗?

无法在32位计算机上运行Golang应用程序

理解Golang中的IOTA和常量

将这两个函数合二为一的惯用方法

Prometheus 摘要分位数错误

为什么 Go 对于长度为 100k 的切片使用的内存比长度为 100k 的数组要少?

为什么我只收到部分错误而不是我启动的 goroutines 的所有错误?

在密钥不存在时处理 PATCH 部分更新

如果值为 false,gRPC 不返回布尔值

Apache Beam 在 Go 中从 PCollection 中 Select 前 N 行

如何将一片 map 转换为一片具有不同属性的 struct

如何在切片增长时自动将切片的新元素添加到函数参数

此代码如何生成内存对齐切片?

如何在测试中使用自定义标志(使用`testify/suite`)

如何通过组合来自不同包的接口来创建接口?

在 Golang 中获取谷歌云服务帐户的访问令牌?

如何使用 fyne 避免 GUI 应用程序中的循环依赖?

如何使用通用字段初始化匿名struct数组