0

I have a Trade struct like so:

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

And an execution struct like so:

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

Schema:

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;

However, when creating executions and then the trade from yaml fixtures with the appropriate relationships, I'm getting an error:

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

Which points to the Preload call I make here:

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

Debugging this has been difficult so trying to get some help on it.

1 Answer 1

1

Define your Trade struct like this

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

Or even

type Trade struct {
    ID              uint
    BuyExecution    Execution
    BuyExecutionID  int
    SellExecution   Execution
    SellExecutionID int
    Px              int
    Shares          int
}
1
  • it's good if you add some comment in your answer, so we get to know why this change? since there many beginners. Commented Aug 28, 2023 at 16:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.