Repository navigation
Reduce CRUD boilerplate code with a convenience mixin #254
Description
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 typing import Optional, Union
from fastapi import FastAPI, Depends, HTTPException
from sqlmodel import Field, Session, SQLModel, create_engine, select
class ActiveRecord(SQLModel):
@classmethod
def by_id(cls, id: int, session):
obj = session.get(cls, id)
if obj is None:
raise HTTPException(status_code=404, detail=f"{cls.__name__} with id {id} not found")
return obj
@classmethod
def all(cls, session):
return session.exec(select(cls)).all()
@classmethod
def create(cls, source: Union[dict, SQLModel], session):
if isinstance(source, SQLModel):
obj = cls.from_orm(source)
elif isinstance(source, dict):
obj = cls.parse_obj(source)
session.add(obj)
session.commit()
session.refresh(obj)
return obj
def save(self, session):
session.add(self)
session.commit()
session.refresh(self)
def update(self, source: Union[dict, SQLModel], session):
if isinstance(source, SQLModel):
source = source.dict(exclude_unset=True)
for key, value in source.items():
setattr(self, key, value)
self.save(session)
def delete(self, session):
session.delete(self)
session.commit()
class HeroBase(SQLModel):
name: str = Field(index=True)
secret_name: str
age: Optional[int] = Field(default=None, index=True)
class HeroCreate(HeroBase):
pass
class HeroRead(HeroBase):
id: int
class HeroUpdate(SQLModel):
name: Optional[str] = None
secret_name: Optional[str] = None
age: Optional[int] = None
class Hero(HeroBase, ActiveRecord, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
connect_args = {"check_same_thread": False}
engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
def get_session():
with Session(engine) as session:
yield session
app = FastAPI()
@app.on_event("startup")
def on_startup():
create_db_and_tables()
@app.post("/heroes/", response_model=HeroRead)
def create_hero(hero: HeroCreate, session: Session = Depends(get_session)):
return Hero.create(hero, session)
@app.get("/heroes/", response_model=list[HeroRead])
def read_heroes(session: Session = Depends(get_session)):
return Hero.all(session)
@app.get("/heroes/{id}", response_model=HeroRead)
def read_heroes(id: int, session: Session = Depends(get_session)):
return Hero.by_id(id, session)
@app.patch("/heroes/{id}", response_model=HeroRead)
def read_heroes(id: int, hero: HeroUpdate, session: Session = Depends(get_session)):
db_hero = Hero.by_id(id, session)
db_hero.update(hero, session)
return db_heroDescription
Hi all,
I'm fairly new to FastAPI and SQLModel, but I really like these libraries. After porting two Flask+SQLAlchemy projects to FastAPI+SQLModel, I noticed that I write lots of boilerplate code to implement basic model functions like create, save, delete or update. I was surprised that the examples in the documentation implement CRUD functionality with functions rather than member functions of the models.
So I wrote a class called ActiveRecord to be used as a mixin for SQLModel classes, which adds these functions and can be reused for different models and projects. In my code above you see it being used with the multi-model Hero example in the documentation, which shortens the remaining code (compare https://sqlmodel.tiangolo.com/tutorial/fastapi/multiple-models)
Using something like this in all of my SQLModel classes where table=True seems so obvious, that I suspect that there's either something I'm missing here, or this is functionality that's really missing in SQLModel. Why doesn't the SQLModel class have such functions? How do you do that? Do you think this is worth contributing? I guess, at least I could clean it up and share it as gist.
Thanks for any feedback,
Boris
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.9.5
Additional Context
No response
Activity
byrman commentedon Feb 28, 2022
Thanks for sharing. However, I would not consider your
ActiveRecordto be a (true) mixin: it's just a subclass ofSQLModel. So, I tried to improve upon your code like this:This failed, however, with:
AttributeError: type object 'ActiveRecord' has no attribute '__config__'. This might be a bug in SQLModel and I will make a pull request for it.bolau commentedon Feb 28, 2022
You're totally right. I ran into the same error, that's why I subclassed it, although it shouldn't be necessary in theory. But I wasn't able to solve this in a cleaner way. If you can fix that, that would be awesome!
byrman commentedon Feb 28, 2022
This pull request would add support for mixins: #256.
deajan commentedon Sep 29, 2022
@bolau I really enjoy your ActiveRecord class.
I really think this should be part of sqlmodel basics, since it really scaffolds REST usage nicely.
@tiangolo Would you consider adding this to the SQLModel base class ?
bolau commentedon Sep 29, 2022
@deajan Great, thanks for letting me know!
Here's an updated version that I currently use:
deajan commentedon Sep 29, 2022
@bolau Thanks, indeed it looks like alot of work went into that boilerplate.
Apart from the pandas function which comment out, it's quite enjoyable.
I still have a quick question. Having to provide a session for each function call is alot of work.
If you're using fastapi's depends() functionality, it might make sense.
But without it, for every member function call we need a
with get_session() as sessioncontext statement before using the ActiveRecord functions.Why not make session optional ?
Eg your current code:
would become
With get_session() being the boring standard, eg:
bolau commentedon Sep 30, 2022
That's a fabulous idea! Having to pass the session annoyed me, too. I was under the impression, that I should really try to use only a single session per HTTP handler for all ActiveRecord actions. But as a FastAPI and SQLModel newbie, I didn't know how to solve that. Your approach is very pragmatic, since it just acquires a session in every call. And maybe that's the right thing to do!
deajan commentedon Sep 30, 2022
This would indeed make all function signatures swap parameters since
sessionmust be the last one since it's optional.I noticed that you already swapped function signatures between
ActiveRecordandActiveRecordMixinclasses.Eg for by_id functions, the signature was
(int, session) -> resultsand became(session, int).Was there any special reason for that ?
Also, we might just use
**kwargsas last argument maybe, something that would look alike:The drawback would be that you could pass virtually any parameter to the class methods without any noticeable errors.
There could also be a solution for this problem, by raising an error when kwargs > 0 once session has been taken from it.
What are your thoughts on this ?
bolau commentedon Sep 30, 2022
I swapped session to be the first parameter, since I passed it everytime - makes the method signatures look
more similar. I'd prefer your session=None over the kwargs. But IMHO, the nicest solution from a user's standpoint would be if the correct session is "just there" as a global object, without passing it explicitly. Similar to flask.request:
So in my ideal world, the ActiveRecord methods should create a new session with
get_sessionif there is none open, and use the existing session when being called inside a session context, but without explicitly receiving the session as an argument.But I don't know how to do that. I've tried it with FastAPI.Depends, but I realized that those don't work that way...
deajan commentedon Sep 30, 2022
The problem with the global object is that you have class methods as well as instance methods mixed in ActiveRecord class, so there would be at least two "global" sessions, one for the instance and one for the class.
So far, here's what I did:
I am thinking of a way to not duplicate the classmethod code to the instance. Any ideas perhaps ?
bolau commentedon Sep 30, 2022
Hm, does this really work? When you use the class in two different handlers, it will use the same session. I thought the session has to be more 'local' to the handler, like this:
Doesn't that matter?
deajan commentedon Sep 30, 2022
Not sure to understand what you mean.
Is the
all()function from earlier ActiveRecord class you posted ?So yes, I'm using the same session for multiple instances of the class.
For the example I gave above, I don't care about getting the session via FastAPI's
Depends(). I just use the session obtained in the object itself. AFAIK there's no reason I'd need to use the FastAPI obtained session instead of the object's one.Maybe there's something I am missing ?
bolau commentedon Sep 30, 2022
.allretrieves all objects from that class, and it's from the newer version.My understanding is, that if you have multiple sessions at the same time and assign them to different classes in the constructor as you propose, that they won't be synced and you end up working with different cached copies of what's actually in the database. That's at least why I'm trying to use only one session in one HTTP handler.
deajan commentedon Sep 30, 2022
Hmm... Indeed, I think that could be solved by using ScopedSessions, ie https://docs.sqlalchemy.org/en/14/orm/contextual.html
But then, your whole program needs to use them. So that's not elegant either.
In the end, the only solution might be to request a new session per operation, ie:
This is a lot of duplicated code, but at least it's for an universal boilerplate.
deajan commentedon Sep 30, 2022
I've tried to find a bit of documentation about session caching.
Didn't find it to be an actual issue to re-use the same session in a class.
More interesting, as of my readings, I found that we're probably trying to reinvent the wheel here:
https://github.com/absent1706/sqlalchemy-mixins/blob/master/sqlalchemy_mixins/activerecord.py
There's already thre above full implementation of ActiveRecords for SQLAlchemy. I do think that with
__config__ = Nonethat should be SQLModel compatible.I'll have a try next week or so
Maybe then your ActiveRecordMixin class enhancements can be added to the existing implementation ?
bolau commentedon Sep 30, 2022
Interesting! I was under the impression, that it would have to be done using SQLModel classes rather than SQLAlchemy, but maybe it works on that level as well.
deajan commentedon Nov 22, 2022
@bolau Never reported back, but indeed project https://github.com/absent1706/sqlalchemy-mixins works like a charm adding ActiveRecord style syntax to SQLModel, as well as Timetamping (created_at, updated_at) and Serialization support.
I'll probably try to catch up with the author for session context integration. Thanks for the chat, and thanks for the idea you posted about SQLModel and ActiveRecords
@tiangolo Integrating https://github.com/absent1706/sqlalchemy-mixins was quite convenient in SQLModel, as long as #256 is applied. Please consider adding this directly into SQLModel as it would greatly improve the productivity ;)
bolau commentedon Nov 28, 2022
Thanks @deajan, that sounds great! Although I already have 3 projects in production with my self-made mixin, it would be nice to have an even sleeker and more complete solution. I will give it a shot! And yes, I'd also really like to see #256 being integrated as an enabler for goodies like this :)
ethagnawl commentedon Mar 10, 2023
@deajan Before I reinvent the wheel trying to get sqlalchemy-mixins working in my application, could you share how you were able to do so? Also, did you ever make any progress on cleanly integrating the session context?
EDIT:
It looks like the answer to my first question might be as simple as:
baojd42 commentedon Mar 31, 2023
I want to write the mixin with
__config__ = None, but it cant not pass the type check.iloveitaly commentedon Apr 9, 2026
👋 I have a wrapper library that has lifecycle hooks, ORM-like DX, easy session management, JSONB mutation tracking, and other rails-like features. Check it out! Would love to hear any feedback and have more contributions to it.