Closed Marahin closed 1 year ago
Before passing the object to pgxscan.Get
After being processed by pgxscan
. Notice how Guild
is no longer nil
, but now instead is a *Guild
, however all its attributes are nil pointers.
Hi @Marahin! Thank you for opening this issue.
The behavior you observe is expected. Scany initializes all nested structs to be able to save results from the database, even NULLs
. I would suggest thinking about nested structs, not as objects (because scany isn't an ORM), but rather as a subset of columns you want to reuse between the queries.
Another popular database scanning library sqlx
does it the same way as scany.
If you need your structs to act as objects — with proper M2M and O2M relations and nils
— you can always transform structs you use with scany to those objects in your application code.
I hope that helps. Let me know if you have any questions.
Thank you for time spent responding to this issue @georgysavva. Indeed, my expectations were exactly how you described them, but I am happy to hear that this behavior is expected. I understand the rationale behind it, it makes sense.
Hi.
I'm having a
Player
struct that has different related tables mapped to structs and nested within (Player
may have multipleDeaths
, and an optionalGuild
). I have noticed that theGuild
relation is returning a pointer to an empty struct (*Guild{}
) instead of a nil pointer. I believe that because of that, when marshaling it into JSON, theGuild
still remains in the response, as an empty object{}
.I would expect that it is completely omitted.
Definition of types:
The SQL query:
The result is something along these lines:
How is this all called?