id never heard of a functional unique constraint but there you go, here's a stack overflow answer for this one:
http://stackoverflow.com/questions/1510018/compound-uniqueconstraint-with-a-function On Nov 10, 2011, at 1:15 AM, lestat wrote: > Can I add UniqueConstraint like this? > > > from sqlalchemy import func > from sqlalchemy.schema import UniqueConstraint > from sqlalchemy.dialects import postgres > > class UserIp(db.Model): > __tablename__ = 'user_ip' > > __table_args__ = ( > UniqueConstraint('user_id', func.md5('ip')), > ) > > user_id = db.Column(db.Integer, db.ForeignKey('user.id'), > primary_key=True, default=None) > ip = db.Column(postgres.CIDR, nullable=False, > default='127.0.0.1/32') > > > > It raise exception: > > /usr/lib/python2.7/site-packages/flaskext/sqlalchemy.pyc in > __init__(self, name, bases, d) > 465 def __init__(self, name, bases, d): > 466 bind_key = d.pop('__bind_key__', None) > --> 467 DeclarativeMeta.__init__(self, name, bases, d) > 468 if bind_key is not None: > 469 self.__table__.info['bind_key'] = bind_key > > /usr/lib/python2.7/site-packages/sqlalchemy/ext/declarative.pyc in > __init__(cls, classname, bases, dict_) > 1257 return type.__init__(cls, classname, bases, dict_) > 1258 else: > -> 1259 _as_declarative(cls, classname, cls.__dict__) > 1260 return type.__init__(cls, classname, bases, dict_) > 1261 > > /usr/lib/python2.7/site-packages/sqlalchemy/ext/declarative.pyc in > _as_declarative(cls, classname, dict_) > 1156 cls.__table__ = table = Table(tablename, > cls.metadata, > 1157 *(tuple(cols) + > tuple(args)), > -> 1158 **table_kw) > 1159 else: > 1160 table = cls.__table__ > > /usr/lib/python2.7/site-packages/sqlalchemy/schema.pyc in __new__(cls, > *args, **kw) > 267 metadata._add_table(name, schema, table) > 268 try: > --> 269 table._init(name, metadata, *args, **kw) > 270 table.dispatch.after_parent_attach(table, > metadata) > 271 return table > > /usr/lib/python2.7/site-packages/sqlalchemy/schema.pyc in _init(self, > name, metadata, *args, **kwargs) > 342 # initialize all the column, etc. objects. done after > reflection to > 343 # allow user-overrides > --> 344 self._init_items(*args) > 345 > 346 @property > > /usr/lib/python2.7/site-packages/sqlalchemy/schema.pyc in > _init_items(self, *args) > 62 for item in args: > 63 if item is not None: > ---> 64 item._set_parent_with_dispatch(self) > 65 > 66 def get_children(self, **kwargs): > > /usr/lib/python2.7/site-packages/sqlalchemy/events.pyc in > _set_parent_with_dispatch(self, parent) > 228 def _set_parent_with_dispatch(self, parent): > 229 self.dispatch.before_parent_attach(self, parent) > --> 230 self._set_parent(parent) > 231 self.dispatch.after_parent_attach(self, parent) > 232 > > /usr/lib/python2.7/site-packages/sqlalchemy/schema.pyc in > _set_parent(self, table) > 1827 > 1828 def _set_parent(self, table): > -> 1829 ColumnCollectionMixin._set_parent(self, table) > 1830 Constraint._set_parent(self, table) > 1831 > > /usr/lib/python2.7/site-packages/sqlalchemy/schema.pyc in > _set_parent(self, table) > 1801 if isinstance(col, basestring): > 1802 col = table.c[col] > -> 1803 self.columns.add(col) > 1804 > 1805 class ColumnCollectionConstraint(ColumnCollectionMixin, > Constraint): > > /usr/lib/python2.7/site-packages/sqlalchemy/sql/expression.pyc in > add(self, column) > 2135 > 2136 """ > -> 2137 self[column.key] = column > 2138 > 2139 def __delitem__(self, key): > > AttributeError: 'Function' object has no attribute 'key' > > > > > Thanks! > > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalchemy@googlegroups.com. > To unsubscribe from this group, send email to > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.