-
Notifications
You must be signed in to change notification settings - Fork 237
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
Left joins with inline and optional relationships? #399
Comments
Hello @cmoad, Can you try setting ParentID to
That way you'll be able to have Also, this looks similar to the case you're trying to solve: The proposal above would do the same with less code, the |
I could but that would require dereferencing the pointer every time the variable is accessed. That isn't ideal and I was hoping for a more general purpose solution. Sqlx supports embedding by prefixing each child relationship with a tag. E.g. The assoc tag looks interesting. I don't fully understand it, but in general I am finding that the most painful thing with sql/go is performing joins across many tables and scanning the results. Particularly with using left joins which yield null results. Sometimes hundreds of lines of code are required to fully scan the results and populate models correctly. Upper looks like it could really help too, but I'm struggling to find the best way. |
@cmoad try type Child struct {
ID int64 `db:"id"`
ParentID sql.NullInt64 `db:"parent_id"`
} |
I've referred to https://tour.upper.io/sql-builder/02 but the example doesn't show how to deal with optional relationships / left joins.
In this made up example, a parent has zero or more children and a child has exactly one parent. (Don't question it. Just go with it.) I want a query that grabs all children for a specific parent.
I'll always get back one or more rows (assuming there is a parent with id = 1), but I can't figure out how to scan into the inline Child without getting an error like:
Is there a clean way to handle this without writing a completely new struct for Child that supports null types?
The text was updated successfully, but these errors were encountered: