fastapi-crudrouter icon indicating copy to clipboard operation
fastapi-crudrouter copied to clipboard

Not populating nested models [Tortoise]

Open marcoaaguiar opened this issue 4 years ago • 9 comments

I found an issue when Tortoise's PydanticModel is used, where the related models are not being populated. Due to Tortoise's async nature and Pydantic lack of asyncio support, some methods need to be called to populate models with nested submodels properly (related link).

I hacked a fix for this in my fork, so we can continue development in my company. I want to clean it up and make a PR when I have some free time, but it seems that there is quite an elaborate test setup. Could you give me some pointers? Maybe I should make a test case apart from the rest, and you could give me the right pointers later when everything is finished.

What do you think?

marcoaaguiar avatar Apr 30 '21 02:04 marcoaaguiar

If you could clean it up and PR it would be great! I would be happy to give you some pointers and help you out.

One thing of note, line 14 on your fork needs to be eventually moved in to the try catch shown below. Otherwise, CRUDrouter will crash for those that do not have tortoise installed.

try:
    from tortoise.models import Model
except ImportError:
    Model: Any = None  # type: ignore
    tortoise_installed = False
else:
    tortoise_installed = True

awtkns avatar Apr 30 '21 06:04 awtkns

Hey @marcoaaguiar, have you done any progress on this issue? Thanks

fullonic avatar May 17 '21 16:05 fullonic

I created several tests of this issue failing. I can PR the WIP, if someone wants to finish it, it would be nice. I haven't had a lot of free time lately.

marcoaaguiar avatar May 17 '21 19:05 marcoaaguiar

@marcoaaguiar, that would be great. I have some issues related to this issue as well but right now I don't have a complete solution to it. If you create a PR with our current work, then we can try to find together the most suitable solution. Cheers

fullonic avatar May 21 '21 09:05 fullonic

@fullonic #68, I opened a WIP PR with the tests and the code changes. Feel free to ask questions

marcoaaguiar avatar May 21 '21 20:05 marcoaaguiar

This error is related to: tortoise.exceptions.NoValuesFetched: No values were fetched for this relation, first use .fetch_related() ??

I'm having this error using related models. This library saved much time, but without it, I will need to make all by hand.

@fullonic , any news about it?

Thanks!!

diegocgaona avatar Aug 28 '21 15:08 diegocgaona

Would be great to pull @marcoaaguiar idea

GonerGenesis avatar Sep 03 '21 13:09 GonerGenesis

Hi @diegocgaona. I still didn't have time to update our changes to make it available. We patched a lot of stuff to fit our needs that is making it hard to make a PR. To fix your issue you can use the PR #68 branch.

fullonic avatar Sep 20 '21 14:09 fullonic

We are no longer using Tortoise in our products, so I will continue with #68. Feel free to continue or close that PR.

marcoaaguiar avatar Oct 08 '21 21:10 marcoaaguiar