gogo-gormgo-fiber

GORM [error] unsupported data type: &[], incorrect schema


GORM v1.25.1, I'm tryping to run DB.AutoMigrate() on Worker, Poster and Job models but running into [error] unsupported data type: &[]. The Worker and Job struct should have a many-to-many relation, while Poster and Job should have one-to-many relation. Worker and experience, worker and preference both should be a one-to-many relation. Please help.

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"`
}

Solution

  • I was able to achieve what you need with the following code.

    Please note that, for the sake of the demo, I simplified your models (structs) by including only the relevant fields for the associations. If I omitted something worth mentioning, feel free to ask for it and I'll update my answer.

    Let me first share the code, then I'll explain it in detail.

    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{})
    }
    

    As your question involve both the many2many and the one2many relationship, I'm gonna divide my answer into two parts.

    The many2many relationship

    To achieve this relationship, you've to add the annotation gorm:"many2many:workers_posters;joinForeignKey:workersId" next to the slices that hold associated entities. The affected fields are:

    Obviously, you've to change the value for the joinForeignKey depending on what field you're about to set.

    The one2many relationship

    This relationship is even simpler as you don't have to specify the join table that has to be created in a many2many association (e.g. the above created workers_posters table). Here, you only have to do these two changes:

    1. Add the Jobs []Job field within the Poster struct
    2. Add the PosterID uint field within the Job struct

    Thanks to this, if you run the Automigrate method, you'll see the right tables in the database with all of the foreign keys correctly set up.

    Let me know, thanks!