sqlacodegen
sqlacodegen copied to clipboard
Output Models to Different Files
Feature Request: would be nice to have the option to output all of the model classes to separate files; saves having to do a lot of cut/pasting when the single output file is huge.
How often do you need to do this, really?
Actually this would be really handy for large projects with lots of tables. Thanks
Would you like to elaborate? One file per class / table? And where would association tables go?
It just seemed like a lot of effort for a tiny bit of convenience. This tool is meant to be run just once when you start writing your models against an existing database.
Thanks for asking. If the tool is meant to be run just once then it's probably not worth the effort but that's not how I use this sort of tool. I like to do my design with database modelling tools and then regenerate the models from the databases when they change (aka database-first).
The tools I've used in the past for code generation (in other object-oriented languages) generate a seperate file for each class probably because 1) They prefer one file per class and 2) It's more manageable when you have a lot of tables. Normally an association table would also have it's own file as it's a separate class.
I realise that in Python it's pretty common to have more than one class in a file and modern programming tools make it easy to move between those classes so it's not essential IMHO.
I realise that in Python it's pretty common to have more than one class in a file and modern programming tools make it easy to move between those classes so it's not essential IMHO.
Exactly. I find that the people who want to separate models to their own modules usually come from other languages like Java (like I did) where you literally have no choice about it.
I am a Python developer, but I'd still like this option. Over several years I've a few times been given the task taking ownership of a large, existing database, and to begin managing it with sqla and alembic. Sometimes there's dozens, or hundreds of tables. I can work with that all in one file, but tbh I'd rather have a hundred files in models/ than one models.py that's possibly thousands of lines long. After the fact, I might choose to reorganize them a bit further, but having the option to start with things broken out would be nice.
I used ChatGPT to create a script that does this quite easily.
I used ChatGPT to create a script that does this quite easily.
Doing flying spaghetti monster's work. Thank you sir/ma'am
It would be useful to have this natively
I made a homemade solution to deal with imports too, maybe it will help someone in the future: sqlacodegen-model-split