-
Notifications
You must be signed in to change notification settings - Fork 13
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
Optional relations #8
Comments
In the code I found the option What I would expect is that the property is set to |
Hi Allard, Great to see you're looking into this again :) You pinpointed the only thing that I'm not happy with. (The rest works great 😄) The big problem is that it's hard to decide if a relation should be set to null. For example
If groups.name is nullable, than we have no way to know if the property group is null, or the name property of group is null ( We could automatically add groups.id to the SELECT, but I want to avoid adding implicit magic. My current idea for v3 is a follows: if an INNER JOIN is used (also on nullable properties), the type of the result will be always be not nullable. Eg
Will result in And for OUTER JOIN's, an extra parameter is added to the outer join functions, where you can specify which column determines the nullability So I think a runtime check can help, so What do you think of this approach? BTW
Runtime this type is passed to the decorator as |
Hi Wouter, Thanks for your answer. I've been thinking a bit more about this over the past few days and my conclusion is mostly in line with what you commented.
I have been trying to build a prototype for this, but typed knex is not an easy project to get started on :-) |
I'd love to make a simple default that just works. But I'm not sure you're solution would work all of the time. For example if users.groupId and groups.name are both nullable, then Can give this result:
Does that mean |
That would be great! I would say:
This makes inner join work 100% correct and also the typing for the outer join. The result for outer joins will not always be correct, as you described, if you only select nullable columns. But I think it would be a great improvement of the current situation where outer joins are almost always wrong, both in typing and in result. |
Just to keep you posted: I started work on this in #11
I see two solutions. The first one is to try to change the model. If an inner join is made, change the property to The other solution would be to have a model and a 'select model' The model is the class that you give to the query, including all foreign key objects. The select model only has the non foreign key objects. The first one feels a bit like a hack. An added benefit to the second one is that it prevents you from adding columns from tables to the select clause which aren't joined yet. It does mean that you have to write joins before select, which is not how normal SQL is written. |
Great! I don't think changing the model is a hack: doing tings like joins or selects does change the type of the result. In a system like TypedKnex the Entity class is basically only a description or configuration for TypedKnex, not a real class like in a classic ORM. But the second options sounds fine as well. It does indeed have some downsides. The order that you have to do selects and joins is different from SQL but makes sense in a programming language like JS. In code I always do the selects last.
This can be fixed most of the time by either chaining or reassigning to the variable of course. When dynamically building the query |
Issue type:
[X] Question
[ ] Bug report
[X] Feature request
[X] Documentation issue
Database system/driver:
[X] Postgres
typed-knex version:
[X]
2.8.1
(or put your version here)Knex.js version:
0.20.3
Steps to reproduce or a small repository showing the problem:
Hi Wouter,
I'm (once more) looking into the Typed Knex project. The current state looks quite nice and I like the new short and concise syntax.
During my first test I ran into a problem, which I think is not solvable with the current version, but might just need a better explanation in the docs.
What I'm trying to achieve is to get optional relations working properly. Suppose the following knex migration:
The simplest Type Knex implementation of this model would be:
But then, performing queries on this:
The result of user2 is really awkward. The fact that user2.group is an object instead of
null
is strange, and the typing of this object is incorrect because both fields in this object should be non-nullable.Is there a way around this? The following does not work:
Thanks!
The text was updated successfully, but these errors were encountered: