2016-01-15 52 views
1

我有以下表格。在Gorm中與不同表格名稱的一對一關係

nyct2010 enter image description here

旅行

enter image description here

我已經定義的模型如下。

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

出於某種原因格姆無視,我映射到Nyct2010.Id,我想它映射到Nyct2010.gid領域。

我正在討論這個錯誤還是Gorm的錯誤?

+0

邊評論:ID應ID。 https://github.com/golang/go/wiki/CodeReviewComments#initialisms –

回答

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") 
+0

那沒用,我得到以下... '''[2016-01-16 10:04:11]無效關聯[欄目:pickup [pickup _nyct2010_gid] ''' –

+0

對不起。查看更新的答案。我會盡可能在同一時間再現你的錯誤。你可以發表一個表創建腳本的SQL? –

+0

我現在得到以下內容。看起來,gorm忽略了正確的字段,而是想用'id'來代替。 '[2016-01-17 18:36:39] [124.81ms] SELECT * FROM「trips」WHERE(「id」='2112111736')ORDER BY「trips」。「id」ASC LIMIT 1' '[2016-01-17 18:36:39] pq:列「id」不存在' '[2016-01-17 18:36:39] [57.39ms] SELECT * FROM「nyct2010」WHERE 「id」='1475') [2016-01-17 18:36:39] pq:列「id」不存在 –

相關問題