Executing this command:
sqlacodegen <connection-url> --outfile db.py
The db.py contains generated tables:
t_table1 = Table(...)
and classes too:
Table2(Base):
__tablename__ = 'table2'
The problem is that a table is generated in one way only - either a table or a class.
I would like to make it generate models (classes) only but in the provided flags I couldn't find such an option. Any idea?
It looks like what you're describing is a feature itself.
sqlacodegenwill not always generate class models.It will only form model classes for tables that have a primary key and are not association tables, as you can see in the source code:
Furthermore, in the documentation it is stated that
Although, you can try a quick and dirty hack. Locate those lines in the source code (something like
/.../lib/python2.7/site-packages/sqlacodegen/codegen.py) and comment out the first three code lines (and fix indentation):I have tried this for one specific table that was generated as a table model. It went from
to
You can also open an issue about this as a feature request, in the official tracker.
Just in case, if you want the opposite (only table models), you could do so with the
--noclassesflag.