Gorm: One-To-One relationship is confusing

Created on 11 Mar 2015  路  2Comments  路  Source: go-gorm/gorm

type Language struct {
  ID      int
  Name    string
  Display string
}

type GameLang struct {
  ID   int
  Game Game2
  Lang Language
}

type Game2 struct {
  ID      int
  Name    string `sql:"unique";"not null"`
  Display string `sql:"not null"`
}

when I

db.CreateTable(&Game2{})
db.CreateTable(&Language{})
db.CreateTable(&GameLang{})

check the game_lang table only ID field created, the game and lang is missing.

I am working with sqlite3 database, anything wrong?

All 2 comments

I figured it out

should add an ID field served as reference id and it should be name convention ?

type GameLang struct {
  ID     int
  Game   Game2
  GameID int64

  Lang   Language
  LangID int64
}

I suggest add this part to doc name convention part

Hi @davyzhang,

This was mentioned in the doc, refer:

type User struct {
    ID           int
    Birthday     time.Time
    Age          int
    Name         string  `sql:"size:255"` // Default size for string is 255, you could reset it with this tag
    Num          int     `sql:"AUTO_INCREMENT"`
    CreatedAt    time.Time
    UpdatedAt    time.Time
    DeletedAt    time.Time

    Emails            []Email         // One-To-Many relationship (has many)
    BillingAddress    Address         // One-To-One relationship (has one)
    BillingAddressID  sql.NullInt64   // Foreign key of BillingAddress
    ShippingAddress   Address         // One-To-One relationship (has one)
    ShippingAddressID int             // Foreign key of ShippingAddress
    IgnoreMe          int `sql:"-"`   // Ignore this field
    Languages         []Language `gorm:"many2many:user_languages;"` // Many-To-Many relationship, 'user_languages' is join table
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

bramp picture bramp  路  3Comments

kumarsiva07 picture kumarsiva07  路  3Comments

izouxv picture izouxv  路  3Comments

alanyuen picture alanyuen  路  3Comments

hypertornado picture hypertornado  路  3Comments