go - 属于关联 Golang Gorm 未找到具有显式 ForeignKey

标签 go go-gorm

我有一个像这样的交易结构:

type Trade struct {
    ID            uint
    BuyExecution  Execution `gorm:"ForeignKey:BuyExecution"`
    SellExecution Execution `gorm:"ForeignKey:SellExecution"`
    Px            int
    Shares        int
}

像这样的执行结构:

type Execution struct {
    ID                uint
    Side              string
    Symbol            string
    Trade             *Trade
}

架构:

CREATE TABLE `executions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `side` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `symbol` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `trade_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



CREATE TABLE `trades` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `buy_execution_id` int(11) NOT NULL,
  `sell_execution_id` int(11) NOT NULL,
  `px` int(11) NOT NULL,
  `shares` int(11) NOT NULL,
  `trade_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

但是,当创建执行然后从具有适当关系的 yaml 固定装置进行交易时,我收到一个错误:

[2016-06-28 21:17:50]  sql: converting Exec argument #0's type: unsupported type models.Execution, a struct 

它指向我在这里进行的 Preload 调用:

var trade Trade
db.Preload("BuyExecution").First(&trade)

调试它一直很困难,所以试图获得一些帮助。

最佳答案

像这样定义你的 Trade 结构

type Trade struct {
    ID              uint
    BuyExecution    Execution `gorm:"ForeignKey:BuyExecutionID"`
    BuyExecutionID  int
    SellExecution   Execution `gorm:"ForeignKey:SellExecutionID"`
    SellExecutionID int
    Px              int
    Shares          int
}

甚至

type Trade struct {
    ID              uint
    BuyExecution    Execution
    BuyExecutionID  int
    SellExecution   Execution
    SellExecutionID int
    Px              int
    Shares          int
}

关于go - 属于关联 Golang Gorm 未找到具有显式 ForeignKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38086702/

相关文章:

go - 如何将标志变量传递给 Golang 中的 http.Get

go - 困惑 : implement multiple interfaces in Go

Go 忽略 HTTP_PROXY 环境变量

mysql - panic : dial tcp 127. 0.0.1:3306: 连接: 连接被拒绝

go - gorm 的自动预加载未按预期工作

go - 没有相关对象的查询

postgresql - 连接时 Gorm + Docker 错误

google-app-engine - 查找方法以获取数据库中返回空数据集的记录

windows - Go中终止进程的跨平台方式

go - 解决 go 1.7 中 vendor 的 'cannot find package' 错误