-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StructScan rows after outer join #162
Comments
+1 I would love to have sqlx allow this syntax: type EmployeeDepartment struct {
Employee
*Department
} |
+1 Current workaround involves a null-object in the database which feels really wrong |
@jmoiron are there any plans to support this in the near future? |
I'm facing the same situation. Would be nice to allow @nubbel proposal. |
+1 Same here. |
I'm facing this issue as well. Honestly I thought it was enough to just use sql.NullInt64 on the possibly-null-foreign-key (to borrow the example above):
However, I get the error: I don't really feel like I should have to have a pointer to Department in the EmployeeDepartment struct. Just zero out the fields and let me check if Edit: I am fine with pointer idea or any idea, but I definitely need a solution to this... |
+1 |
1 similar comment
+1 |
Yeah, this is definitely a pain point for me. In general the columns in my tables are not nullable which means I have non-nulltable fields in my scannable structs. However, when I start doing joining I end up with all sorts of null fields meaning I need to either make my struct fields all nullable (and dealing with that mess in my code), or duplicating the structs for scanning the joined rows. |
I've been able to work around this by making all of the fields in the nillable struct nillable themselves as well, but it isn't pretty since it will actually instantiate the B struct on an empty row, but all of its fields will be empty.
|
If you are using PostgreSQL you can use Example:
Notice how |
+1 |
Is this something that can be supported? LEFT OUTER JOIN is pretty common. Would it be bad for sqlx to just check if the resulting join is null, and if its, do not instantiate the struct? This seems pretty standard for a struct serialization library with join support. |
This should be supported, in my opinion. It practically blocks scanning results of LEFT JOIN queries that might yield no matching value. |
@jmoiron This would be pretty nice to add to SQLx. I can put together a PR if we agreed on the behavior, what do you think? |
@yousseftelda Did you ever make that pr? If not, I might make a stab at it. |
Is there a way to simply embed e.g. "right" table into "left" table and make "LEFT OUTER JOIN"?
Columns on the right can be nil, but I don't feel that copy-pasting type definition replacing types with their Null counterparts is good idea.
I tried to perform two separate scans and put them together:
The text was updated successfully, but these errors were encountered: