Skip to content

Loading an object from the database (vs init) produces different field types - it shouldn't #110

Description

@tasn

First Check

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

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import sqlmodel as sqlm

class BarType(str):
    @property
    def with_prefix(self):
        return f"PREFIX! {self}"

class Foo(SQLModel, table=True):
    id: str = sqlm.Field(primary_key=True)

    bar: BarType

# Works

foo = Foo(id="some-id", bar="test")
assert isinstance(foo.bar, BarType)

# Though this fails:

foo = load_foo_from_db()
assert isinstance(foo.bar, BarType) # FAILS!

Description

The types are currently lying when loading from db. :)

Wanted Solution

They should work.

Wanted Code

Some thing as above, but works.

Alternatives

No response

Operating System

Linux

Operating System Details

No response

SQLModel Version

0.0.4

Python Version

3.9

Additional Context

No response

Activity

  1. changed the title [-]When loading an object from the database, the fields should be loaded according to their type[/-] [+]Loading an object from the database (vs init) produces different field types - it shouldn't[/+] on Sep 23, 2021
  2. jack-michaud commented on Oct 7, 2021

    @jack-michaud

    What is the type of foo.bar when you load it from the database?

  3. tasn commented on Oct 7, 2021

    @tasn
    Author

    str

    You can check out https://github.com/tiangolo/sqlmodel/blob/main/sqlmodel/main.py#L372 to see exactly what's going on.

    To workaround this issue, I have a fork (which is what we use) that lets you customize the type in a few ways, this is one of them: https://github.com/tasn/sqlmodel/blob/fixes/sqlmodel/main.py#L378

    Essentially what I do is create a custom string loader sqlalchemy type that instantiates the value after loading from the db (have the same for other base types), and I just force its usage here.

  4. jp-u commented on Mar 10, 2024

    @jp-u

    This is still an issue on data returned from the database, seemingly for fields that use anything other than builtins.

  5. locked and limited conversation to collaborators on May 18, 2026
  6. converted this issue into a discussion #1934 on May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions