Django and FastAPI: One To One Relationships
Main differences between Django and FastAPI from a data engineering perspective
13 min readApr 24, 2024
Not a Medium member? Use the Friend Link to read this article!
After we went through the fundamental differences between defining database objects with Django and SQLAlchemy (Medium article), it is time to deep dive into relationships.
Key Differences In Model Definition
- If there is no primary key defined on the model, Django will automatically generate one for you. SQLAlchemy will raise an error;
- In Django the columns are by default not null, while on SQLAlchemy they are by default nullable;
- Django makes a distinction between NULL and empty string, which involves insertion of empty string on not null fields (the behaviour can be fixed by cleaning the data in the save method);
- Django introduced only recently default values at database level, through db_default, while in SQLAlchemy is called
server_default
; - Django does not have yet the option to delegate the delete cascade at database level (feature request), while in SQLAlchemy this can be achieved by setting
passive_deletes=True
in the relationship definition (docs);