{"id":10254,"library":"sqlalchemy-repr","title":"SQLAlchemy Repr","description":"sqlalchemy-repr, currently at version 0.1.0, is a lightweight Python library that automatically generates human-readable `__repr__` methods for SQLAlchemy declarative models. It aims to simplify debugging and logging by providing informative string representations of model instances without manual boilerplate. The project is stable but has a low release cadence.","status":"active","version":"0.1.0","language":"en","source_language":"en","source_url":"https://github.com/manicmaniac/sqlalchemy-repr","tags":["sqlalchemy","repr","debugging","orm"],"install":[{"cmd":"pip install sqlalchemy-repr","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for ORM model integration.","package":"SQLAlchemy","optional":false}],"imports":[{"symbol":"ReprMixin","correct":"from sqlalchemy_repr import ReprMixin"}],"quickstart":{"code":"from sqlalchemy import create_engine, Column, Integer, String\nfrom sqlalchemy.orm import sessionmaker\nfrom sqlalchemy.ext.declarative import declarative_base\nfrom sqlalchemy_repr import ReprMixin\n\n# 1. Setup SQLAlchemy Base\nBase = declarative_base()\n\n# 2. Define a model with ReprMixin\nclass User(ReprMixin, Base):\n    __tablename__ = 'users'\n    id = Column(Integer, primary_key=True)\n    name = Column(String)\n    email = Column(String)\n\n    # Optional: Customize which attributes are shown in repr\n    _repr_values = ['id', 'name']\n\n# 3. Create an engine and tables\nengine = create_engine('sqlite:///:memory:')\nBase.metadata.create_all(engine)\n\n# 4. Create a session\nSession = sessionmaker(bind=engine)\nsession = Session()\n\n# 5. Create and add an instance\nuser1 = User(id=1, name='Alice', email='alice@example.com')\nuser2 = User(id=2, name='Bob', email='bob@example.com')\nsession.add_all([user1, user2])\nsession.commit()\n\n# 6. Demonstrate the custom repr\nretrieved_user = session.query(User).filter_by(name='Alice').first()\nprint(f\"User instance repr: {retrieved_user}\")\n# Expected output (approx): User(id=1, name='Alice')\n\n# Clean up\nsession.close()","lang":"python","description":"This quickstart demonstrates how to integrate `ReprMixin` with a SQLAlchemy declarative model. By inheriting `ReprMixin` alongside `Base`, your model instances will automatically gain a sensible string representation. The example also shows how to customize the included attributes using `_repr_values`."},"warnings":[{"fix":"Monitor the project's GitHub repository for updates if using very new SQLAlchemy versions. Be prepared to implement custom `__repr__` methods or fork the library for compatibility if encountering issues with future SQLAlchemy releases.","message":"The project `sqlalchemy-repr` is stable but has a low level of active development. While it works reliably with existing SQLAlchemy versions, new SQLAlchemy features or major API changes might not be immediately supported.","severity":"gotcha","affected_versions":"0.1.0"},{"fix":"Explicitly define the `_repr_values` attribute on your model to control which columns are included in the representation. For example: `_repr_values = ['id', 'username']` to exclude sensitive fields like `password_hash`.","message":"By default, `ReprMixin` includes all mapped columns in the `__repr__` output. For models containing sensitive data (e.g., passwords, API keys, private information), this could lead to unintended exposure in logs or during debugging.","severity":"gotcha","affected_versions":"all"},{"fix":"When defining models with such relationships, consider carefully setting `_repr_values` to exclude relationship attributes. For very complex cases, implement a custom `__repr__` method to ensure controlled and efficient output.","message":"For models with complex, deeply nested, or circular relationships, relying solely on `ReprMixin`'s default behavior can lead to overly verbose representations or even infinite recursion if relationships are eagerly loaded.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install the package using pip: `pip install sqlalchemy-repr`","cause":"The `sqlalchemy-repr` package is not installed in your current Python environment.","error":"ModuleNotFoundError: No module named 'sqlalchemy_repr'"},{"fix":"Ensure `ReprMixin` is listed *before* `Base` in the inheritance order for your model: `class MyModel(ReprMixin, Base):`","cause":"This error can occur if `ReprMixin` is not the *first* class inherited alongside SQLAlchemy's `Base` in a multiple inheritance scenario, causing conflicts with SQLAlchemy's initialization process (especially in older SQLAlchemy versions).","error":"TypeError: object.__init__() takes exactly one argument (the instance)"},{"fix":"Verify your SQLAlchemy declarative base setup, table definitions, and that `Base.metadata.create_all(engine)` has been called correctly or that tables exist in your database. Ensure the model inherits from `Base`.","cause":"This usually indicates an issue with the SQLAlchemy declarative setup itself, rather than `ReprMixin`. `ReprMixin` requires a properly configured SQLAlchemy declarative model.","error":"sqlalchemy.exc.NoReferenceError: Can't find reference to the table 'tablename' in the database (or similar SQLAlchemy-related error)"}]}