1 year ago

#386359

test-img

Hoang Viet Nguyen

GORM Associations Preload not work with primary key ID int = 0

I have two models below

type Bot struct {
  gorm.Model
  Name           string
  ExchangeID     uint
  Exchange       Exchange
}

type Exchange struct {
  gorm.Model
  Image        string
  Status       string
}

It means a Bot belongs to an Exchange.

In my database, i have a row with id = 0 in table "exchanges" and many rows with exchange_id = 0 in table "bots".

The issue is that Preload in Bot queries not work for rows that have exchange_id = 0. It works well with rows that have exchange_id = 1,2,3...

Has anyone met this issue? I think it's gorm issue with special value 0. I'm working around by declaring ExchangeId in Bot and Id in Exchange as string but it has some tradeoffs.

go

go-gorm

0 Answers

Your Answer

Accepted video resources