Reputation: 516
go version
)?Go version Go 1.9.1 Linux/amd64
sqlite3
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?
Upvotes: 0
Views: 423
Reputation: 5770
You data is not normalized. Config
is a struct with multiple fields. You can extract config into a separate table with a foreign key, like you did with B
:
type B struct {
ID int
AID int
Config Config `gorm:"foreignkey:BID"`
}
And then define the foreign key in Config
:
type Config struct {
BID int
attr1 int
attr2 string
}
At the end, you create the table:
Db.CreateTable(&Config{})
Upvotes: 1