BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
2.05k stars 217 forks source link

Issue with fetch_all_links() #537

Closed roman-right closed 1 year ago

roman-right commented 1 year ago

Discussed in https://github.com/roman-right/beanie/discussions/512

Originally posted by **vvladic** March 22, 2023 fetch_all_links() breaks when some objects in list[Link] are already fetched we check here if ref_obj is a link, ``` ref_obj = getattr(self, field, None) if isinstance(ref_obj, Link): <--- value = ref_obj.fetch(fetch_links=True) setattr(self, field, value) if isinstance(ref_obj, list) and ref_obj: values = Link.fetch_list(ref_obj, fetch_links=True) setattr(self, field, values) ``` perhaps we should to the same in Link.fetch_list() instead of just assuming the list contains just links. ``` def fetch_list(cls, links: List["Link"], fetch_links: bool = False): ids = [] model_class = None for link in links: if not isinstance(link, Link): <--- continue if model_class is None: model_class = link.model_class else: if model_class != link.model_class: raise ValueError( "All the links must have the same model class" ) ids.append(link.ref.id) return model_class.find(In("_id", ids), with_children=True, fetch_links=fetch_links).to_list() # type: ignore ```
Abashinos commented 1 year ago

Just hit this bug while testing around the compatibility one 😅

roman-right commented 1 year ago

Oops. The previous (deleted) comment was for another issue. This one will be fixed in the PR: https://github.com/roman-right/beanie/pull/571