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: two usability issues with sqlalchemy #354

Merged
merged 5 commits into from
Jan 11, 2025
Merged

Conversation

Askir
Copy link
Contributor

@Askir Askir commented Jan 10, 2025

I am fixing two quite annoying issues with the SQLAlchemy integration in this PR:

  1. It wasn't possible to have the same field name configured on two different entities. So you can't have content_embeddings twice, because the name was colliding in the Mapper configuration. So I changed the dynamically generated Model name to contain the parent Model.

  2. The way we were configuring the relationship into the other direction via backref was somewhat lazily evaluated and is also no longer recommended by SQLAlchemy. It's considered "legacy". I changed it with some back and forth to be simply two relationships in both directions and added a parent_kwargs field that allows to configure the relationship into the reverse direction.

@Askir Askir requested a review from a team as a code owner January 10, 2025 16:51
@Askir Askir force-pushed the jascha/sqlalchemy-fixes branch from 1de28b4 to 38c7a93 Compare January 10, 2025 16:55
@Askir Askir force-pushed the jascha/sqlalchemy-fixes branch 2 times, most recently from bef36f7 to f1eabb6 Compare January 10, 2025 16:59
Copy link
Contributor

@adolsalamanca adolsalamanca left a comment

Choose a reason for hiding this comment

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

Looks good, just left a small comment.

Comment on lines 152 to 164
def test_can_build_select():
"""
This is a very minimal test case that failed when doing
some development with the extension.
The nature of the vectorizer_relationship being a descriptor messes
with sqlalchemys relationship resolution.
It was previously using `backref` to propagate the parent field,
which is resolved later and an immediate access
to build queries like this would fail.
"""
select(Document.content_embeddings).options(
joinedload(Document.content_embeddings.parent) # type: ignore
) # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess it's intended not to have assertions here, to just verify it builds?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, it was throwing an exception before 😅

Copy link
Collaborator

@cevian cevian left a comment

Choose a reason for hiding this comment

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

Is there a test for issue 1 added somewhere? (I might have missed it)

@Askir Askir changed the title fix: fix usage issues with sqlalchemy fix: usage issues with sqlalchemy Jan 10, 2025
@Askir Askir force-pushed the jascha/sqlalchemy-fixes branch from f1eabb6 to beafcc3 Compare January 10, 2025 22:56
@Askir
Copy link
Contributor Author

Askir commented Jan 11, 2025

@cevian yes the select test covers this too, because the same file has two models with content_embeddings.

@Askir Askir changed the title fix: usage issues with sqlalchemy fix: two usability issues with sqlalchemy Jan 11, 2025
@Askir Askir merged commit 95fa797 into main Jan 11, 2025
5 checks passed
@Askir Askir deleted the jascha/sqlalchemy-fixes branch January 11, 2025 05:05
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.

3 participants