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

Prefetching a model with JSON list causes "TypeError: unhashable type: 'list'" #1401

Open
jannyware-inc opened this issue Oct 24, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@jannyware-inc
Copy link

jannyware-inc commented Oct 24, 2024

Describe the bug
When I prefetch_related() a model that has JSON lists (instead of JSON dicts) I get TypeError.

To Reproduce

  1. Create a model with a JSON field as list such as:
class Album(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="albums")

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100, nullable=True)
    shops: List[Shop] = ormar.ManyToMany(to=Shop, through=AlbumShops)
    sides: list = ormar.JSON(default=list)
  1. Prefetch_related it
track = await Track.objects.prefetch_related(
        ["album__cover_pictures", "album__shops__division"]
).get(title="The Bird")

error:

    @pytest.mark.asyncio
    async def test_prefetch_related_with_many_to_many():
        async with base_ormar_config.database:
            async with base_ormar_config.database.transaction(force_rollback=True):
                div = await Division.objects.create(name="Div 1")
                shop1 = await Shop.objects.create(name="Shop 1", division=div)
                shop2 = await Shop.objects.create(name="Shop 2", division=div)
                album = Album(name="Malibu")
                await album.save()
                await album.shops.add(shop1)
                await album.shops.add(shop2)

                await Track.objects.create(album=album, title="The Bird", position=1)
                await Track.objects.create(
                    album=album, title="Heart don't stand a chance", position=2
                )
                await Track.objects.create(album=album, title="The Waters", position=3)
                await Cover.objects.create(title="Cover1", album=album, artist="Artist 1")
                await Cover.objects.create(title="Cover2", album=album, artist="Artist 2")

>               track = await Track.objects.prefetch_related(
                    ["album__cover_pictures", "album__shops__division"]
                ).get(title="The Bird")

tests\test_relations\test_prefetch_related.py:381:

Expected behavior
It should support JSON dicts and lists, not just dicts

Screenshots
image

Versions (please complete the following information):

  • Database backend used (mysql/sqlite/postgress)
    aiosqlite
  • Python version
    3.12
  • ormar version
    -0.20.1
  • pydantic version
    pydantic==2.5.3
    pydantic-extra-types==2.9.0
    pydantic_core==2.14.6
  • if applicable fastapi version
    fastapi==0.111.0
    fastapi-cli==0.0.4

Additional context

@alex-immersive
Copy link

@jannyware-inc Did you manage to find a workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants