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
```
Discussed in https://github.com/roman-right/beanie/discussions/512