sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

Can't use set in model_obj.dict(exclude=...)

Open by-Exist opened this issue 2 years ago • 2 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the SQLModel documentation, with the integrated search.
  • [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to SQLModel but to Pydantic.
  • [X] I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

None

Description

(using google translate...)

In pydantic, the dict() function argument exclude supports set and dict. However, it seems that the case of set is not available in sqlmodel. That problem seems to be coming from _calculate_keys. Is set explicitly unsupported? Or am I missing something?

Operating System

Windows

Operating System Details

No response

SQLModel Version

0.0.4

Python Version

3.9.6

Additional Context

No response

by-Exist avatar Sep 02 '21 09:09 by-Exist

I am also getting this. @tiangolo any update on this? Works fine with Pydantic

NitroEvil avatar Sep 16 '21 09:09 NitroEvil

To add a bit more flavor, the error is:

File /.../venv/lib/python3.9/site-packages/sqlmodel/main.py:638, in SQLModel._calculate_keys(self, include, exclude, exclude_unset, update)
    635     keys = self.__fields__.keys()  # | self.__sqlmodel_relationships__.keys()
    637 if include is not None:
--> 638     keys &= include.keys()
    640 if update:
    641     keys -= update.keys()

AttributeError: 'set' object has no attribute 'keys'

You get this if you do something like

class Hero(SQLModel):
    id: int

hero = Hero(1)
hero.dict(exclude={'id'})

Also effects the include param.

JLHasson avatar May 17 '22 03:05 JLHasson