you need to turn your __table_args__ into a callable:
@declared_attr
def __table_args__(cls):
return (Index(..., func.lower(cls.name), ...), )
or just use a string for your functional index: Index(...,
text("LOWER(name)"), ...)
On Apr 13, 2014, at 9:22 PM, Joshua Ma <[email protected]> wrote:
> Is there a way to create a functional index in a declarative model without
> referencing the actual column? I currently have something like
>
> class MyModel(db.Base):
> name = db.Column('name', db.String(255))
> __table_args__ = (
> Index('mymodel_lower_name_idx', func.lower(name),
> postgresql_ops={'name': 'text_pattern_ops'}),
> )
>
> which is slightly inconvenient because I usually declare all my columns under
> __table_args__.
>
> More importantly, though, I use a mixin for some models, with the
> __table_args__ in the model and the column in the mixin:
>
> class MyMixin(object):
> name = db.Column(db.String(64), nullable=False)
>
> class MyModel(MyMixin, db.Base):
> __table_args__ = (
> Index('mymodel_lower_name_idx', func.lower(MyMixin.name),
> postgresql_ops={'name': 'text_pattern_ops'}),
> )
>
> but this doesn't seem to work, I get "ArgumentError: __contains__ requires a
> string argument" at runtime.
>
> The rest of my indices are just strings (e.g. Index('mymodel_name_idx',
> 'name')) - is there a similar way to create functional ones? I tried
> 'lower(name)' but it doesn't like that either since it's not a valid column
> (KeyError: 'lower(name)').
>
> Thanks!
> Josh
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.