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

calling sqlalchemy.orm.aliased on inherited SQLModel throws exception sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type <class 'sqlmodel.main.SQLModelMetaclass'> #71

Open
8 tasks done
zangell44 opened this issue Sep 1, 2021 · 1 comment
Labels
question Further information is requested

Comments

@zangell44
Copy link

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

from sqlmodel import SQLModel, Field
from typing import Optional
from sqlalchemy.orm import aliased

class MyModel(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)

class MyNextModel(MyModel):
    name: str = Field(default="marvin")

# works fine
aliased(MyModel, name="my_model_alias")
# throws exception
aliased(MyNextModel, name="my_next_model_alias")

Description

I would expect to be able to alias a SQLModel object that inherits from another SQLModel object.

Running the example code produces the following error

  File "alias_bug.py", line 14, in <module>
    aliased(MyNextModel, name="my_next_model_alias")
  File "/opt/homebrew/Caskroom/miniconda/base/envs/py38/lib/python3.8/site-packages/sqlalchemy/orm/util.py", line 1236, in aliased
    return AliasedClass(
  File "/opt/homebrew/Caskroom/miniconda/base/envs/py38/lib/python3.8/site-packages/sqlalchemy/orm/util.py", line 494, in __init__
    insp = inspection.inspect(mapped_class_or_ac)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/py38/lib/python3.8/site-packages/sqlalchemy/inspection.py", line 71, in inspect
    raise exc.NoInspectionAvailable(
sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type <class 'sqlmodel.main.SQLModelMetaclass'>

Operating System

macOS

Operating System Details

No response

SQLModel Version

0.0.4

Python Version

3.8

Additional Context

No response

@zangell44 zangell44 added the question Further information is requested label Sep 1, 2021
@ljluestc
Copy link

ljluestc commented Sep 3, 2023

from sqlmodel import SQLModel, Field
from typing import Optional

class MyModel(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)

class MyNextModel(MyModel):
name: str = Field(default="marvin")

Create instances for querying (similar to aliasing)

my_model_alias = MyModel(id=1)
my_next_model_alias = MyNextModel(id=2, name="Alice")

print(my_model_alias)
print(my_next_model_alias)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants