Golang GORM无效关联

I'm trying to write a very simple belongsTo association with GORM, but with primary keys that isn't Id.

My structs are as such:

type State struct {
    FIPS string `gorm:"type:char(2);primary_key;column:FIPS"`
    Name string `gorm:"not null"`
    Area float64 `gorm:"type:real;not null"`
}

type ZipCode struct {
    ZipCode   string `gorm:"type:char(5);primary_key;"`
    Name      string `gorm:"not null"`
    State     State `gorm:"ForeignKey:StateFIPS;AssociationForeignKey:FIPS"`
    StateFIPS string `gorm:"type:char(2);column:state_FIPS;not null"`
}

and with the following code:

var zc ZipCode
var s State
db.Model(&zc).Related(&s)

I get the error: [2017-05-18 14:26:13] invalid association [] and a find on the zipcode doesn't load the state. Does GORM not like non-Id primary keys or am I missing something?

With your current code :

var zc ZipCode
var s State
db.Model(&zc).Related(&s)

You are not set anything to your zc variable. so that's why you get an error of invalid association [] with an empty data.

To fix this you must get the ZipCode data from your database like :

db.First(&zc, 1) // find ZipCode with id 1.

and then you can associate your zc full code would be :

var zc ZipCode
var s State

db.First(&zc, 1) // find ZipCode with id 1.
db.Model(&zc).Related(&s)

Note : I'm not testing this actual code but I think it would fix the problem.