Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a model to model association but without any dependency?

Tags:

go

go-gorm

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.

like image 478
Berti92 Avatar asked Dec 17 '21 22:12

Berti92


People also ask

What is difference between Has_one and Belongs_to?

They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user .

What is Rails polymorphic association?

Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.

What is association in Ruby on Rails?

In Rails, an association is a connection between two Active Record models. Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for authors and a model for books.

What is dependent destroy in Rails?

Dependent is an option of Rails collection association declaration to cascade the delete action. The :destroy is to cause the associated object to also be destroyed when its owner is destroyed.


1 Answers

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
}
like image 145
dave Avatar answered Oct 21 '22 22:10

dave