Skip to content
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

fix #336: detecting relationship as nullable=False #458

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

indiVar0508
Copy link
Contributor

Attempt to address #336,

using direction mapping to decide if nullable is to be set False or not

pytest result for new branch: 94 passed, 55 warnings
pytest in master : 94 passed, 55 warnings

@@ -348,7 +348,7 @@ def _add_relationship_kwargs(self, kwargs, prop):
nullable = True
for pair in prop.local_remote_pairs:
if not pair[0].nullable:
if prop.uselist is True:
if not self.DIRECTION_MAPPING[prop.direction.name]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be handling 2 cases:

  1. For anything which has a list - we expect an empty list - and so nullable=False
  2. And for anything which has a FK on the table itself - we should consider the nullable from the FK (MANYTOONE case)
if not pair[0].nullable:
    if prop.uselist is True or self.DIRECTION_MAPPING[prop.direction.name] is False:
        nullable = False
continue

MANYTOONE case:

class Book:
    ...
    author_id = Column(Integer, ForeignKey('author.id'), nullable=False)
    author = relationship('Author', lazy='selectin')

MANYTOMANY case with uselist=True:

class Book:
    ...
    author = relationship('Author', secondary=book_author, lazy='selectin', uselist=True)

MANYTOMANY case with uselist=False:

class Book:
    ...
    author = relationship('Author', secondary=book_author, lazy='selectin', uselist=False)

Can we add testcases for various direction X uselist scenarios ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @AbdealiJK ,

Have updated the changes based on the feedback in comment

@indiVar0508 indiVar0508 marked this pull request as draft September 7, 2022 13:23
@indiVar0508 indiVar0508 marked this pull request as ready for review September 11, 2022 15:25
@lafrech
Copy link
Member

lafrech commented Feb 23, 2023

@indiVar0508 could you please rebase this?

@AbdealiLoKo review welcome after rebase.

Thanks guys!

Copy link
Member

@lafrech lafrech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase again to get the latest changes (SQLAlchemy 2.0).

Thanks.

@@ -354,6 +354,49 @@ class ModelWithArray(Base):
assert field.dump_only is True


class TestRelationShipKwarg:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestRelationshipKwarg

def test_many_to_one_relationship_kwarg(self, models):
default_converter = ModelConverter()

rel = models.Student.__mapper__.get_property("current_school")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use get_property but .attrs (see #495).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants