In this project, we have restricted imports between packages as follows:
- We have a shared package.
- We have a backend package.
- Imports from the backend package to the shared package are not allowed, but imports from the shared package to the backend package are permitted. I cannot change this config.
- Currently, some backend models, specifically SQLModel classes, need to be used in the shared package.
Here’s the solution I have implemented (paths are shortened):
in shared/models.py
:
class SharedBaseSQLModel(SQLModel):
# some shared fields like id
# customized methods like save and read for shared package
class Job(SharedBaseSQLModel): # without table=True
__abstract__ = True
# some fileds like name, status
in backend/models.py
:
from sqlmodel import SQLModel
from shared.models import Job as SharedJob
class BaseSQLModel(SQLModel):
# customized methods like save and read for backend
class Job(BaseSQLModel, SharedJob, table=True): # here I use table=True
# some extra methods
In shared
I need to be able to do this query:
statement = select(Job).where(Job.name == 'foo').order_by(...).limit(...)
return session.exec(statement).all()
Error happens here: select(Job)
-
Current error:
sqlalchemy.exc.ArgumentError: Column expression, FROM clause, or other columns clause element expected, got <class '..._shared.core.model.Job'>
-
Python 3.10.14
-
sqlalchemy-spanner==1.7.0
-
sqlmodel==0.0.18
As far as I understood, If I cannot use table=True
, I cannot use select(MyModel)
.
Do you have any suggestions on how to solve this problem? I’m also open to ideas for a better model design to handle situations like this.