1

I want to declare foreign key relation between tables using XORM in golang for postgres.

I've tried the following schema to create foreign key relationship.

type Author struct {
    Id uint64 `xorm:"pk autoincr"`
    Name string `xorm:"VARCHAR(128) NOT NULL"`
    Email string `xorm:"VARCHAR(128) NOT NULL UNIQUE"`
    Phone string `xorm:"VARCHAR(32)"`
    Age float32
    Address string `xorm:"VARCHAR(256)"`

    Created time.Time `xorm:"created"`
    Updated time.Time `xorm:"updated"`
}


type Book struct {
    Id uint64 `xorm:"pk autoincr"`
    Name string `xorm:"varchar(128) NOT NULL"`
    Isbn string `xorm:"varchar(64) NOT NULL"`
    Author *Author `xorm:"-"`

    Created time.Time `xorm:"created"`
    Updated time.Time `xorm:"updated"`
}

And then,

err := engine.Sync(new(models.Author))
err = engine.Sync(new(models.Book))

But the above code doesn't create foreign key, even no column named author in Book table. Help me to solve this problem that how i can declare foreign key for postgres.

  • It seems to not exist: https://github.com/go-xorm/xorm/issues/73 – RickyA May 06 '19 at 15:16
  • also see https://stackoverflow.com/questions/29435783/gorm-golang-orm-associations – RickyA May 06 '19 at 15:18
  • @RickyA their github page has been moved to another platform, but here's an issue reply from a month ago where they explicitly say they dont support foreign keys right now https://gitea.com/xorm/xorm/issues/1627 – MoralCode May 09 '20 at 20:25

0 Answers0