1

我有以下表格。

nyct2010 在此处输入图像描述

旅行

在此处输入图像描述

我定义的模型如下。

type Nyct2010 struct {
    Id      int `gorm:"column:gid"`
    Borocode int
}

type Trip struct {
    Id               int
    PickupLongitude  float64   `gorm:"column:pickup_longitude"`
    PickupLatitude   float64   `gorm:"column:pickup_latitude"`
    DropoffLongitude float64   `gorm:"column:dropoff_longitude"`
    DropoffLatitude  float64   `gorm:"column:dropoff_latitude"`
    PickupTime       time.Time `gorm:"column:pickup_datetime"`
    DropoffTime      time.Time `gorm:"column:dropoff_datetime"`
    Fare             float64   `gorm:"column:fare_amount"`
    Tip              float64   `gorm:"column:tip_amount"`
    Total            float64   `gorm:"column:total_amount"`
    PaymentType      string    `gorm:"column:payment_type"`
    Tax              float64   `gorm:"column:mta_tax"`

    Nyct2010   Nyct2010
    Nyct2010Id int `gorm:"column:pickup_nyct2010_gid"`
}

我正在尝试从中获取相关条目nyct2010。它与pickup_nyc2010_gid.

var trip Trip
db.First(&trip, 2112111736)

db.Model(trip).Related(&trip.Nyct2010)

上面的代码产生以下调试消息。

[2016-01-15 12:34:04]  [160.31ms]  SELECT  * FROM "trips"  WHERE ("id" = '2112111736') ORDER BY "trips"."id" ASC LIMIT 1

[2016-01-15 12:34:04]  pq: zero-length delimited identifier at or near """" 

[2016-01-15 12:34:04]  [77.29ms]  SELECT  * FROM "nyct2010"  WHERE ("" = '1475')

[2016-01-15 12:34:04]  pq: zero-length delimited identifier at or near """" 

由于某种原因,gorm 忽略了我要映射Nyct2010.Id到的字段,我正在尝试将其映射到Nyct2010.gid.

我是要解决这个问题还是 Gorm 的错误?

4

1 回答 1

0

尝试这个:

type Nyct2010 struct {
    ID        uint `gorm:"primary_key column:gid"`
    Borocode  int
}

var trip Trip
var nyct2010 Nyct2010

db.First(&trip, 2112111736)
db.Model(&trip).Related(&nyct2010,"pickup_nyct2010_gid")
于 2016-01-15T22:08:42.363 回答