dqwh1119 2016-01-15 17:45
浏览 52

表单中具有不同表名的一对一关系

I have the following tables.

nyct2010 enter image description here

and

trips

enter image description here

The models I have defined are below.

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"`
}

I am trying to get the related entry from nyct2010. It is related to pickup_nyc2010_gid.

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

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

The above code produces the following debug messages.

[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 """" 

For some reason gorm is ignoring the field that I am mapping Nyct2010.Id to, I am trying to map it to Nyct2010.gid.

Am I going about this wrong or is this an error with Gorm?

  • 写回答

1条回答 默认 最新

  • douyou9923 2016-01-15 22:08
    关注

    Try this:

    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")
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用lstm来预测股票价格
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上