I’m working on a pet project for a web shop that uses SQLAlchemy (SA) and connects to a database with over 25 tables (thus, I have 25+ SA models – one for each table).
What would be the best practices for managing SA models code for databases with numerous ( >20 ) tables? Would it be more preferable to leave all models in a single file? Or to split models into separate files? What are the pros and cons of either decision except obvious “1 heavy file” vs. “zillion smaller ones”?
To be more precise: as of now, all of SA models in my project are stored in a single file – here, ~650 lines. I’ve also detected a couple of bad design decisions (e.g., violating 3NF in a few places), so there will be even more tables. The DB schema for about half of these tables is as follows:
In the project, all the models lie in models.py
this way:
<project_name>/
│
├── <other folders / files / .. >
│
└── modules/ # Application modules
├── <other modules>
└── db/
├── <other folders and .py scripts>
└── models.py # Here lie all 26 SQLAlchemy models, and counting...
├── SA model 1: User
├── SA model 2: RecentlyViewedProduct
├── SA models 3 to 26
└── <SA models to be added>
Max Azatian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.