-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
SQLAlchemy Relationships fail when using secondary Table #19
Comments
I might take a stab at fixing this maybe next week, but as a workaround, you can get this working by adding a |
+1, I ran into the same issue |
I don't get any error with an association table, however I don't get the result either.
The result I get with such a query @strawberry.type
class QueryEntry:
@strawberry.field
def entry(self, info, entry_id: uuid.UUID) -> EntryGQL | None:
with info.context['session_factory']() as session:
db_entry = session.query(Entry).where(Entry.id == entry_id).one_or_none()
# print(len(db_entry.tags)) # (1)
return db_entry Gets me this result
But if I un-comment the python comment number (1), I get this result.
So it looks like lazy loading is not working but the link works. |
I wanted to check in on the status of this issue, as it is a problem I am encountering myself, and I may have a solution. For context, I have an ORM like so: categoryParentChild = Table(
"category_parent_child",
. . .
Column("parent_id", ForeignKey("category.id")),
Column("child_id", ForeignKey("category.id")),
)
class Category(Base):
. . .
parent: MappedColumn[Category] = relationship(
"category",
secondary="category_parent_child",
primary_join="category.c.id == category_parent_child.c.child_id",
secondary_join="category.c.id == category_parent_child.c.parent_id",
uselist=False,
)
. . . Looking at loader.py:66-73 query = select(related_model).filter(
tuple_(
*[remote for _, remote in relationship.local_remote_pairs or []]
).in_(keys)
)
if relationship.order_by:
query = query.order_by(*relationship.order_by)
rows = await self._scalars_all(query) this seems to be the culprit. Say I wanted to get For a primary join, this works, but we don't have a primary join, we have a secondary join, which contains not one, but two select *
from category, category_parent_child -- note the lack of an 'on' clause
where (category_parent_child.child_id, category_parent_child.parent_id) in (:keys) Without an 'on' clause, we get no means of relating the target to the secondary table, and, as such, fail. The |
Looks like someone beat me to identifying this issue, but I'm not seeing the changes from this merge. |
Did you solved this issue? Have same problem and think as you |
@Ninefiveblade not yet. I can raise a PR with the fix, though. |
Hi, @fruitymedley , I'm not a maintainer but I really support you to create a PR with the fix. This problem is around for a while and resolve this will be very good. |
Hey! Thanks for your great work on this!
I've queried SQLAlchemy Relationships with this library which works just fine, but fails when I use a secondary table. Example:
My query is something like this:
The library tries to look for
account_id
in thePractice
table rather than the secondary table. I've also attached part of a stack trace below:Upvote & Fund
The text was updated successfully, but these errors were encountered: