dsfds4551
dsfds4551
2018-05-18 07:19

为什么Gorm在CreateTable时会忽略结构?

  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术

What version of Go am I using (go version)?

Go version Go 1.9.1 Linux/amd64

Which database and its version am I using?

sqlite3

A complete runnable program to reproduce my issue:

Need to runnable with GORM's docker compose config or please provide your config.

package main

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
)

type A struct {
    ID      int
    Bs              [] *B `gorm:"foreignkey:AID"`
}

type B struct {
    ID      int
    AID     int
    Config          Config `gorm:"type:text"`
}

type Config struct {
    attr1   int
    attr2   string
}


func main() {
    Db, err := gorm.Open("sqlite3", "test.db")
    if err != nil {
        panic(err)
    }
    Db.CreateTable(&A{})
    Db.CreateTable(&B{})
}

However, the schema of test.db is

sqlite> .schema
CREATE TABLE "as" ("id" integer primary key autoincrement );
CREATE TABLE "bs" ("id" integer primary key autoincrement,"a_id" integer );

As we can see, B's config attribute was not created. So Why Gorm is ignoring the Config struct?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐