{"id":10212,"library":"schemainspect","title":"Schema Inspect","description":"Schema Inspect is a Python library for programmatically inspecting database schemas, with a strong focus on PostgreSQL. It integrates with SQLAlchemy to provide a structured representation of tables, columns, constraints, and other schema objects. The current version is 3.1.1663587362, which uses a unique timestamp-based versioning scheme, reflecting its last significant update in late 2022.","status":"maintenance","version":"3.1.1663587362","language":"en","source_language":"en","source_url":"https://github.com/djrobstep/schemainspect","tags":["database","schema","inspection","postgresql","sqlalchemy","introspection"],"install":[{"cmd":"pip install schemainspect","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Provides PostgreSQL-specific introspection capabilities.","package":"pgspecial","optional":false},{"reason":"Core ORM and database toolkit for database interaction.","package":"sqlalchemy","optional":false},{"reason":"SQL parser for various SQL dialects.","package":"sqlparse","optional":false}],"imports":[{"note":"The primary entry point for schema inspection.","symbol":"inspect","correct":"from schemainspect import inspect"}],"quickstart":{"code":"from schemainspect import inspect\nimport os\n\n# Example for PostgreSQL connection string\n# Replace with your actual database URL or use an environment variable\ndsn = os.environ.get(\"DATABASE_URL\", \"postgresql://user:password@localhost:5432/mydb\")\n\n# Inspect the database schema\nprint(f\"Inspecting schema for: {dsn.split('@')[-1] if '@' in dsn else dsn}\")\nschema = inspect(dsn)\n\n# Print all table names\nprint(\"\\nTables found:\")\nif schema.tables:\n    for table in schema.tables:\n        print(f\"  - {table.name}\")\n        # Optionally print columns for the first table as an example\n        if table == list(schema.tables.values())[0]: # Get the first table\n            for column in table.columns:\n                print(f\"    - Column: {column.name}, Type: {column.type}, Nullable: {column.nullable}\")\nelse:\n    print(\"  No tables found.\")\n\n# Access a specific table (replace 'your_table_name' with an actual table in your DB)\n# if 'your_table_name' in schema.tables:\n#     my_table = schema.tables['your_table_name']\n#     print(f\"\\nDetails for table '{my_table.name}':\")\n#     for column in my_table.columns:\n#         print(f\"  Column: {column.name}, Type: {column.type}, Primary Key: {column.primary_key}\")\n","lang":"python","description":"This quickstart demonstrates how to connect to a database using a DSN (Data Source Name) and use `schemainspect` to retrieve and print the names of all tables and basic column details from the inspected schema. It uses an environment variable for the DSN for security."},"warnings":[{"fix":"Always test `schemainspect` thoroughly after updating, especially if relying on specific schema inspection behaviors.","message":"The library uses timestamp-based versioning (e.g., 3.1.1663587362) instead of semantic versioning. This means minor updates could potentially introduce behavioral changes without a major version increment, making it harder to track breaking changes.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Thoroughly test schema inspection results if using a database other than PostgreSQL. Refer to SQLAlchemy's dialect documentation for specifics of your chosen database.","message":"While the project aims to support 'possibly others,' its strong dependency on `pgspecial` and the project's focus imply a primary and most robust support for PostgreSQL. Full feature parity for other database backends (e.g., specific data types, constraints, or unique database objects) is not explicitly guaranteed and might require explicit testing.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Explicitly check for view types if your database contains views and you need to inspect them. For advanced view introspection, consider using database-specific tools or raw SQLAlchemy reflection if `schemainspect` provides insufficient detail.","message":"Views might not be fully supported or behave differently than tables, leading to unexpected results or incomplete metadata when iterating `schema.tables` or accessing `.columns` for view-like objects.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install the library using pip: `pip install schemainspect`","cause":"The `schemainspect` library is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'schemainspect'"},{"fix":"Install the appropriate database driver for your database. For PostgreSQL, run: `pip install psycopg2-binary`. For MySQL, use `pip install mysqlclient`.","cause":"While `schemainspect` depends on SQLAlchemy, it does not directly install database drivers (like `psycopg2` for PostgreSQL, `mysqlclient` for MySQL, etc.) which are required by SQLAlchemy to connect to your specific database.","error":"sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgresql"},{"fix":"Verify the exact table name and its casing by iterating through `schema.tables.keys()` to see all available table names. For example: `print(schema.tables.keys())`.","cause":"The specified table name either does not exist in the inspected schema, or there's a case sensitivity mismatch. Database object names are often case-sensitive or automatically lowercased/uppercased depending on the database and DDL.","error":"KeyError: 'your_table_name' when accessing schema.tables['your_table_name']"}]}