这是indexloc提供的服务,不要输入任何密码
Skip to content

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

@jannyware-inc

Description

@jannyware-inc

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions