-
Notifications
You must be signed in to change notification settings - Fork 227
Make relationship arrays nonnull and required #252
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
Make relationship arrays nonnull and required #252
Conversation
3b94fe4
to
5bed67f
Compare
How is the status of this PR? |
Agreed! Isn't that sort of work the value-premise of this library? |
We have stopped using this library (and python completely actually) so if someone wants to continue off my work feel free to do so in a new PR! |
Dear fellow travelers: I believe this would accomplish the check described in this PR description: from sqlalchemy import inspect
def is_relationship_nullable(model, relationship_name: str) -> bool:
"""
Return True if this is a singular relationship and any of the foreign-keys
on the local side are nullable.
"""
mapper = inspect(model)
relationship = mapper.relationships[relationship_name]
if relationship.uselist:
return False
return any(
pair[0].nullable
for pair in relationship.local_remote_pairs
) While I will probably not open a PR into this project, I will probably be using a code snippet |
I ended doing a monkey patch (not sure if a PR would work at the project seems inactive):
|
@treasuryspring If you're interested, I would review and merge your PR including this feature and the corresponding tests ASAP! |
See #251
It will be harder (maybe impossible?) to make the Child -> Parent relationship Non-null, because there is not a single way to determine whether or not it's non-null.
In this example class
We can know for sure that parent is non-null because the parent_id column is nullable=False, however if it used
primaryjoin=xxx
orforeign_keys=[key1,key2]
, We would have to introspect those and check all the possible columns for nullability.However, for the O2M or the M2M cases, SQLAlchemy returns a collection of objects: https://docs.sqlalchemy.org/en/13/orm/collections.html#customizing-collection-access
(It looks like you can also actually return a dictionary mapping instead of a colleciton, however graphene-sqlalchemy currently doesn't support this use case anyway since it hardcodes List)