{"library":"sqlalchemy-schemadisplay","title":"SQLAlchemy SchemaDisplay","type":"library","description":"SQLAlchemy SchemaDisplay is a Python library (version 2.0) for generating visual diagrams from SQLAlchemy ORM models or directly from a database schema. It leverages the Graphviz engine to produce high-quality visualizations of database structures, showing tables, columns, relationships, and data types. Releases are primarily driven by feature additions and compatibility updates with SQLAlchemy.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install sqlalchemy-schemadisplay graphviz"],"cli":{"name":"schemadisplay","version":"sh: 1: schemadisplay: not found"}},"imports":["from sqlalchemy_schemadisplay import create_schema_graph"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/fschulze/sqlalchemy_schemadisplay","docs":"https://github.com/fschulze/sqlalchemy_schemadisplay/blob/master/README.rst","changelog":null,"pypi":"https://pypi.org/project/sqlalchemy-schemadisplay/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import os\nfrom sqlalchemy import create_engine, Column, Integer, String\nfrom sqlalchemy.orm import declarative_base\nfrom sqlalchemy_schemadisplay import create_schema_graph\n\n# 1. Define your SQLAlchemy models\nBase = declarative_base()\n\nclass User(Base):\n    __tablename__ = 'users'\n    id = Column(Integer, primary_key=True)\n    name = Column(String(50), nullable=False)\n    email = Column(String(100), unique=True)\n\nclass Product(Base):\n    __tablename__ = 'products'\n    id = Column(Integer, primary_key=True)\n    name = Column(String(50), nullable=False)\n    price = Column(Integer)\n\n# 2. Create the graph and save it to a file\ntry:\n    # Using Base.metadata directly\n    graph = create_schema_graph(\n        metadata=Base.metadata,\n        show_datatypes=True,\n        show_labels=True,\n        rankdir='LR', # Left-to-right layout\n        orientation='portrait'\n    )\n    output_file = 'schema.png'\n    graph.write_png(output_file)\n    print(f\"Schema diagram saved to {output_file}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    print(\"Make sure Graphviz is installed on your system PATH and 'graphviz' Python package is installed.\")\n\n# Optional: Clean up (if you created a temporary database for reflection)\n# os.remove('test.db') if os.path.exists('test.db') else None","lang":"python","description":"This quickstart demonstrates how to define simple SQLAlchemy models using `declarative_base` and then use `create_schema_graph` to generate a PNG diagram representing their structure. It outputs a file named `schema.png` in the current directory. Remember that the Graphviz executable must be installed on your system for this to work.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}