diff --git a/docs/eav.rst b/docs/eav.rst new file mode 100644 index 0000000..83121f5 --- /dev/null +++ b/docs/eav.rst @@ -0,0 +1,5 @@ +EAV helpers +=========== + +.. automodule:: sqlalchemy_utils.eav + diff --git a/docs/index.rst b/docs/index.rst index a3c7e7f..3fda2ac 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -13,6 +13,7 @@ SQLAlchemy-Utils provides custom data types and various utility functions for SQ aggregates decorators generic_relationship + eav database_helpers model_helpers license diff --git a/sqlalchemy_utils/eav.py b/sqlalchemy_utils/eav.py index 3d24d6b..611ee1b 100644 --- a/sqlalchemy_utils/eav.py +++ b/sqlalchemy_utils/eav.py @@ -1,3 +1,49 @@ +""" +SQLAlchemy-Utils provides some helpers for defining EAV_ models. + + +.. _EAV: + http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model + +:: + + + from sqlalchemy_utils import MetaType, MetaValue + + + class Product(self.Base): + __tablename__ = 'product' + id = sa.Column(sa.Integer, primary_key=True) + name = sa.Column(sa.Unicode(255)) + + + class ProductAttribute(self.Base): + __tablename__ = 'product_attribute' + id = sa.Column(sa.Integer, primary_key=True) + data_type = sa.Column( + MetaType({ + 'unicode': sa.UnicodeText, + 'int': sa.Integer, + 'datetime': sa.DateTime + }) + ) + name = sa.Column(sa.Unicode(255)) + + + class ProductAttributeValue(self.Base): + __tablename__ = 'product_attribute_value' + id = sa.Column(sa.Integer, primary_key=True) + + product_attr_id = sa.Column( + sa.Integer, sa.ForeignKey(ProductAttribute.id) + ) + product_attr = sa.orm.relationship(ProductAttribute) + + value = MetaValue('product_attr', 'data_type') + + +""" + from inspect import isclass import sqlalchemy as sa from sqlalchemy.ext.hybrid import hybrid_property