sqlacodegen icon indicating copy to clipboard operation
sqlacodegen copied to clipboard

Output Models to Different Files

Open d-w-d opened this issue 6 years ago • 10 comments

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.

d-w-d avatar Feb 19 '19 04:02 d-w-d

How often do you need to do this, really?

agronholm avatar Jul 02 '20 05:07 agronholm

Actually this would be really handy for large projects with lots of tables. Thanks

canine-afficionado avatar Oct 29 '21 06:10 canine-afficionado

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.

agronholm avatar Oct 29 '21 06:10 agronholm

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.

canine-afficionado avatar Oct 29 '21 10:10 canine-afficionado

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.

agronholm avatar Oct 29 '21 10:10 agronholm

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.

nixjdm avatar Aug 31 '23 19:08 nixjdm

I used ChatGPT to create a script that does this quite easily.

ab5y avatar Sep 19 '23 20:09 ab5y

I used ChatGPT to create a script that does this quite easily.

Doing flying spaghetti monster's work. Thank you sir/ma'am

devspacenine avatar Nov 28 '23 23:11 devspacenine

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

GuilhermeLimaSP avatar Feb 11 '24 03:02 GuilhermeLimaSP