Home > Back-end >  How to specify a model to model association but without any dependency?
How to specify a model to model association but without any dependency?

Time:12-18

i got two models in GORM as following:

    type (
    Todo struct {
        gorm.Model
        EventId     int64      `json:"event_id" form:"event_id" binding:"required"`
        UserId      uint       `json:"user_id" form:"user_id"`
        DoneUserId  uint       `json:"done_user_id" form:"done_user_id"`
        Groups      string     `json:"groups" form:"groups"`
        DoneAt      *time.Time `json:"done_at" form:"done_at"`
        TodoEnd     time.Time  `json:"todo_end" form:"todo_end" binding:"required"`
        Priority    uint       `json:"priority" form:"priority" binding:"required"`
        Done        bool       `json:"done" form:"done"`
        Title       string     `json:"title" form:"title" binding:"required"`
        Description string     `json:"description" form:"description"`
        CreatorId   uint       `json:"creator_id"`
        ChangerId   uint       `json:"changer_id"`
        Event       Event      
    }
)

and

type (
    Event struct {
        gorm.Model
        CustomerId    uint      `json:"customer_id" form:"customer_id" binding:"required"`
        AddressId     uint      `json:"address_id" form:"address_id" binding:"required"`
        UserId        uint      `json:"user_id" form:"user_id"`
        EventType     string    `json:"event_type" form:"event_type" binding:"required"`
        ContactPerson string    `json:"contact_person" form:"contact_person"`
        Title         string    `json:"title" form:"title" binding:"required"`
        Description   string    `gorm:"type:text" json:"description" form:"description"`
        Calculated    bool      `json:"calculated" form:"calculated"`
        Goodwill      bool      `json:"goodwill" form:"goodwill"`
        Billable      bool      `json:"billable" form:"billable"`
        EventBegin    time.Time `json:"event_begin" form:"event_begin" binding:"required"`
        EventEnd      time.Time `json:"event_end" form:"event_end" binding:"required"`
        PartsJson     string    `gorm:"type:text" json:"parts_json" form:"parts_json"`
        FieldsJson    string    `gorm:"type:text" json:"fields_json" form:"fields_json"`
        CreatorId     uint      `json:"creator_id"`
        ChangerId     uint      `json:"changer_id"`
        Todos         []Todo
        Customer      Customer
    }
)

When I save a new Todo with an event_id set, then I get many errors regarding empty fields within the event object. Thats right, because I do not fill the event object I just set the event_id in the todo object. So my question is, is there a way how I can disable these validations?

The association from Todo to Event is just for query reasons, that I get a nested Event-Object in Todo-Object - or better said I get the nested object in the json.

CodePudding user response:

I would personally try do it like this:

type MinimalTodo struct {
    gorm.Model
    EventId     int64      `json:"event_id" form:"event_id" binding:"required"`
    UserId      uint       `json:"user_id" form:"user_id"`
    DoneUserId  uint       `json:"done_user_id" form:"done_user_id"`
    Groups      string     `json:"groups" form:"groups"`
    DoneAt      *time.Time `json:"done_at" form:"done_at"`
    TodoEnd     time.Time  `json:"todo_end" form:"todo_end" binding:"required"`
    Priority    uint       `json:"priority" form:"priority" binding:"required"`
    Done        bool       `json:"done" form:"done"`
    Title       string     `json:"title" form:"title" binding:"required"`
    Description string     `json:"description" form:"description"`
    CreatorId   uint       `json:"creator_id"`
    ChangerId   uint       `json:"changer_id"`
}

func (MinimalTodo) TableName() string {
    return "todos"
}

type Todo struct {
    MinimalTodo
    Event
}

Not sure if it'll work with Gorm. Otherwise, I'd probably see how much work it would be to just change Event to a pointer:

type Todo struct {
    gorm.Model
    EventId     int64      `json:"event_id" form:"event_id" binding:"required"`
    UserId      uint       `json:"user_id" form:"user_id"`
    DoneUserId  uint       `json:"done_user_id" form:"done_user_id"`
    Groups      string     `json:"groups" form:"groups"`
    DoneAt      *time.Time `json:"done_at" form:"done_at"`
    TodoEnd     time.Time  `json:"todo_end" form:"todo_end" binding:"required"`
    Priority    uint       `json:"priority" form:"priority" binding:"required"`
    Done        bool       `json:"done" form:"done"`
    Title       string     `json:"title" form:"title" binding:"required"`
    Description string     `json:"description" form:"description"`
    CreatorId   uint       `json:"creator_id"`
    ChangerId   uint       `json:"changer_id"`
    Event       *Event
}

CodePudding user response:

Try with nullable types, for example

sql.NullString
sql.NullInt16

More info on nullable types https://pkg.go.dev/database/sql

  • Related