This project is deprecated! SQLAlchemy already manages __eq__ and
__hash__ based on identity within the session. This project is incompatible
with attrs 17.1.0+ or recent versions of SQLAlchemy.
If you're just looking for a nice __repr__ for your SQLAlchemy models,
consider sqlalchemy-repr, repr, or other packages.
Use the amazing attrs library to add
__repr__, __eq__, __cmp__, and __hash__ methods according to
the fields on a SQLAlchemy model class.
from attrs_sqlalchemy import attrs_sqlalchemy
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
@attrs_sqlalchemy
class MyModel(Base):
__tablename__ = 'mymodel'
id = sa.Column(sa.Integer, primary_key=True)
text = sa.Column(sa.String)
instance = MyModel(id=1, text='hello')
same_data = MyModel(id=1, text='hello')
same_pk = MyModel(id=1, text='world')
assert instance == same_data
assert instance != same_pk
assert repr(instance) == "MyModel(id=1, text='hello')"$ pip install attrs_sqlalchemyattrs_sqlalchemy is released under the MIT license, its code lives on GitHub, and the latest release on PyPI.