0

在 STI 结构上预加载嵌套关系时,我得到了一些奇怪的 SQL:

公司结构:

type Firm struct {
    gorm.Model
    FirmIdentifier   string
    Name             string
    Type             string
    ClearingMember   *Firm `gorm:"ForeignKey:ClearingMemberID"`
    ClearingMemberID int
    ExecutingFirm    *Firm `gorm:"ForeignKey:ExecutingFirmID"`
    ExecutingFirmID  int
    Address          Address `gorm:"polymorphic:Addressable;"`
    Contacts         []Contact
}

type Execution struct {
    ID                uint
    Price             int
    Side              string
    Symbol            string
    ExecutingFirm     Firm `gorm:"ForeignKey:FirmID"`
    FirmID            int

}

以下是实际数据:

-
  id:                 1
  name:               Clearing Member 1
  type:               ClearingMember
  clearing_member_id: 

-
  id:                 3
  name:               Executing Firm 1
  type:               ExecutingFirm
  clearing_member_id: 1
-
  # Execution
  id: 1
  side: SELL
  price: 1000
  symbol: FUT.EXC.201612
  firm_id: 3

这个查询:

db.Preload("ExecutingFirm").Preload("ExecutingFirm.ClearingMember").Find(&executions)

生成以下 SQL,它查找 aclearing_member_id为 3 的公司,而不是 ID 为 1 的公司:

(/home/vagrant/go/src/message_builder_test.go:90) 
[2016-09-15 18:30:33]  [1.14ms]  SELECT * FROM `executions`  

(/home/vagrant/go/src/message_builder_test.go:90)  
[2016-09-15 18:30:33]  [1.51ms]  SELECT * FROM `firms`  WHERE `firms`.deleted_at IS NULL AND ((`id` IN ('3','4')))

(/home/vagrant/go/src/message_builder_test.go:90)  
[2016-09-15 18:30:33]  [1.24ms]  SELECT * FROM `firms`  WHERE `firms`.deleted_at IS NULL AND ((`clearing_member_id` IN ('3','4')))
4

0 回答 0