GORM Playground Link
I found out I got different results using ScanRows or Scan.
if I use:
err=models.DB.ScanRows(nodes, &node)
I get empty values in some fields.
If I write each field of this node structure, and I use scan: err = nodes.Scan(&node.NodoId, &node.NodoIdPadre, &node.TipoNodo, &node.SubFormulaId, &node.FuncionId, &node.TipoTerminoId, &node.TerminoId,&node.FuncionDesc, &node.CodFormula, &node.TipoTerminoDesc, &node.DescTipoNodo, &node.OperadorId, &node.Operador ) I get the right values
It does not have any NULL value, I replace these Null values with coalesce function.
nodes, err := models.DB.Model(&Node{}).Raw(WITH RECURSIVE tree AS
(
SELECT id, 0 nodoPadre
FROM nodo
WHERE "nodoPadreId" is null
AND "formulaId"=? and "eliminadoEn" IS NULL
UNION ALL
SELECT o.id,n.id nodoPadre
FROM nodo o
JOIN tree n on n.id = o."nodoPadreId"
WHERE o."eliminadoEn" IS NULL
)
SELECT t.id as "Id",t.nodoPadre as "nodoPadre",coalesce(n."tipoNodoId",0) as "tipoNodoId",coalesce(n."codigoSubFormulaId",0), coalesce(n."funcionId",0),
coalesce(n."tipoTerminoId",0),coalesce(n."terminoId",0),
COALESCE(t1.descripcion, '') as "funcDesc",coalesce(f."codigoFormula",0), COALESCE(t2.descripcion,'') as "tipoTermDesc",
COALESCE(t3.descripcion, '') as "descTipoNodo",
coalesce( n."operadorId",0 ) as OperadorId, COALESCE( t4.descripcion,'') as "operador"
FROM tree t, nodo n
LEFT JOIN "tablaAuxiliarDetalle" t1 ON n."funcionId" = t1.id
LEFT JOIN formula f ON n."formulaId" = f.id
LEFT JOIN "tablaAuxiliarDetalle" t2 ON n."tipoTerminoId" = t2.id
LEFT JOIN "tablaAuxiliarDetalle" t3 ON n."tipoNodoId" = t3.id
LEFT JOIN "tablaAuxiliarDetalle" t4 ON n."operadorId" = t4.id
WHERE
t.id=n.id and n."eliminadoEn" IS NULL
ORDER BY nodoPadre,t.id, version.FormulaId).Rows()
This is my node struct
type Node struct { NodoId int NodoIdPadre int TipoNodo int SubFormulaId int FuncionId int TipoTerminoId int TerminoId int FuncionDesc string CodFormula int TipoTerminoDesc string DescTipoNodo string OperadorId int Operador string }
Thank you
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨