These are chat archives for jinzhu/gorm

13th
Apr 2017
Akshat
@ak5hat_twitter
Apr 13 2017 06:08
hey guys
i want to add CreatedBy column to a Model
and want make it populate automatically using db logged in username
Maksim Levental
@makslevental
Apr 13 2017 16:56
what's wrong with this definition of a model with a foreign key?
package models

import (
    "database/sql"
    "time"
)

type (
    // Organizational unit within volume containing related files (with common annotations),
    // often corresponding to an individual data session (single visit/acquisition/participant/group/day).
    Container struct {
        ContainerID uint `json:"container_id" gorm:"column:id; primary_key"`

        Volume   Volume         `gorm:"ForeignKey:VolumeFk"` // foreign key to volume
        VolumeFk uint           `json:"container_volume_fk" gorm:"column:volume"`
        Name     sql.NullString `json:"container_name" gorm:"column:name"`
        Top      bool           `json:"container_top" gorm:"column:top"` // default false
        Date     time.Time      `json:"container_date" gorm:"column:date"`
    }
)
when i do db.Model(&container).Related(&volume) I get the zero value
Maksim Levental
@makslevental
Apr 13 2017 17:07
actually that's not true. that does work. what doesn't work is filling in the Volume struct when query for a &container