Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy: Auto generate class definitions for a legacy database

How can I auto-generate class definitions for SQLAlchemy, including relationships and backref, as the Django-ORM does with manage.py inspectdb?

like image 856
Harper Avatar asked Sep 14 '17 15:09

Harper


1 Answers

You can use the python package sqlacodegen for exactly this purpose.

sqlacodegen will inspect the database and generate SQLAlchemy style class definitions for future reference and store them in a file.

https://pypi.python.org/pypi/sqlacodegen

Usage:

  • pip install sqlacodegen
  • Start from command line: sqlacodegen [connection string] [options]

The generated class definitions are largely correct and include index definitions and constraints (foreign keys).

In my case, it saved me from typing out 19.000 rows of class definitions.

like image 140
Harper Avatar answered Nov 03 '22 23:11

Harper