go-pg / pg

Golang ORM with focus on PostgreSQL features and performance
https://pg.uptrace.dev/
BSD 2-Clause "Simplified" License
5.67k stars 404 forks source link

Multiple joins not deserializing correctly #1998

Closed farbodg closed 8 months ago

farbodg commented 8 months ago

I have the following tables:

type Table1 struct {
  tableName models.TableName `pg:"table1"`

  ID string `pg:"id,pk"`
  Table2ID string `pg:"table2_id,notnull"`
  ...

  Table2 *Table2 `pg:"rel:has-one"`
  Table3 *Table3 `pg:"rel:has-one"` // can be has-many
}
type Table2 struct {
  tableName models.TableName `pg:"table2"`

  ID string `pg:"id,pk"`
  ...
}
type Table3 struct {
  tableName models.TableName `pg:"table3"`

  ID string `pg:"id,pk"`
  Table1ID string `pg:"table1_id,notnull"`
  ...

  Table1 *Table1 `pg:"rel:has-one"`
}

And the following query:

return tx.Model(&table1).
  ColumnExpr("table2.*").
  ColumnExpr("table3.*").
  Relation("Table2").
  Relation("Table3").
  Join("INNER JOIN table2").
  JoinOn("table1.table2_id = table2.id").
  Join("INNER JOIN table3").
  JoinOn("table1.id = table3.table1_id").
  Where(...).
  Select()

My results return the records in Table1 and Table2, but not Table3 (Table3 field is nil).