Skip to content

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

Open
@tasn

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

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