uptrace / bun

SQL-first Golang ORM

Home Page:https://bun.uptrace.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Has Many Join on nullable value

comsma opened this issue · comments

type InventoryReceiptsLine struct {
	bun.BaseModel `bun:"table:inventory_receipts_line"`

	ReceiptNumber             float64         `bun:"receipt_number,type:decimal(19,0),pk"`
	InvMastUid                int32           `bun:"inv_mast_uid,type:int"`

	Ira []*InvTran `bun:"rel:has-many,join:receipt_number=sub_document_no,join:inv_mast_uid=inv_mast_uid,join:type=trans_type,polymorphic:IRA"`
}

type InvTran struct {
	bun.BaseModel `bun:"table:inv_tran"`

	TransType              string          `bun:"trans_type,type:varchar(5)"`
	SubDocumentNo   sql.NullFloat64 `bun:"sub_document_no,type:decimal(19,0),nullzero"`
	InvMastUid             int32           `bun:"inv_mast_uid,type:int"`
}

I am trying to join a list of transactions(InvTran) to InventoryReceiptsLine. Being that InvTran.SubDocumentNo is sql.NullFloat64 i get an error saying *errors.errorString: bun: has-many relation=Ira does not have base model=InventoryReceiptsLine with id=[{%!q(float64=5.17761e+06) %!q(bool=true)} '華'] (check join conditions).

How can i join these without changing the type of SubDocumentNo as these models are base on a active erp system in which i do not have control over the schema?

Oh wow, the timing is good. Exact same issue for me.

type Individual struct {
	bun.BaseModel  `bun:"individuals,alias:ind"`
	ID             string             `json:"ID" required:"true" bun:",pk"`
	WorkspaceID    string             `json:"WorkspaceID" required:"true"`
	HubspotObjects []*HubspotObject   `json:"HubspotObjects" bun:"rel:has-many,join:id=individual_id"`
}

type HubspotObject struct {
	bun.BaseModel  `bun:"hubspot_objects,alias:hso"`
	ID             string          `json:"ID" required:"true" bun:",pk"`
	WorkspaceID    string          `json:"WorkspaceID" required:"true"`
	IndividualID   *string         `json:"IndividualID"` // can be null
	OrganizationID *string         `json:"OrganizationID"` // can be null
	SyncedAt       *time.Time      `json:"SyncedAt"`
	UpdatedAt      time.Time       `json:"UpdatedAt" required:"true"`
	CreatedAt      time.Time       `json:"CreatedAt" required:"true"`
}

func (h *Handlers) SearchIndividuals() {
	ids := []string{"a", "z"}

	// load the individuals
	individuals := []entity.Individual{}
	if err := h.bun.NewSelect().
		Model(&individuals).
		Relation("HubspotObjects").
		Where("ind.id IN (?)", bun.In(ids)).
		Scan(ctx); err != nil {
		return nil, apperrors.SqlError(err, "search individuals")
	}
}

And it errors: errors.errorString: bun: has-many relation=HubspotObjects does not have base model=Individual with id=[%!q(*string=0x1400003d2b0)] (check join conditions)