SQLAlchemy: Auto generate class definitions for a legacy database

后端 未结 1 850
南笙
南笙 2021-01-06 07:40

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

相关标签:
1条回答
  • 2021-01-06 08:00

    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.

    0 讨论(0)
提交回复
热议问题