On Wed, Nov 7, 2018 at 7:34 PM Luke <[email protected]> wrote:
>
> Michael and others:
>
> My goal is to have an orm Mixin that implements everything for Postgresql 
> tsvector full-text search
> that I can import as a single object into a new project.
>
> I haven't been able to get a event.listen( ... trigger DDL ...) to work as 
> part of a Mixin. I don't really
> understand the docs on __declare_last__, so I'm working from some 
> stackoverflow answers. The code
> creates the expected Index and Trigger if the statements are defined outside 
> of the class.
>
> ```
> class Searchable:
>     """ An SQLAlchemy ORM Mixin that creates Postgres TSVECTOR columns
>
>     :example:
>     >>> class Text(Searchable, Base):
>     >>>     pass
>
>     """
>
>     # Original inspiration:  
> http://shisaa.jp/postset/postgresql-full-text-search-part-1.html
>
>     # __abstract__ = True
>
>     _trigger_ddl = DDL(
>         "create trigger ts_update before insert or update on text for "
>         "each row execute procedure tsvector_update_trigger(tsvector, "
>         "'pg_catalog.english', 'text');"
>     )
>
>     # Data Fields
>     text = Column(String)
>
>     # PostgreSQL Full Text Search field
>     # http://www.postgresql.org/docs/current/static/datatype-textsearch.html
>     tsvector = Column(postgresql.TSVECTOR)
>
>     @declared_attr
>     def __table_args__(cls):
>         # CREATE INDEX tsvector_idx ON tsvector USING 
> gin(to_tsvector('english', message));
>         # Note: __table_args__ should return a tuple
>         return (Index(
>             "tsvector_idx_%s" % cls.__tablename__,
>             "tsvector",
>             postgresql_using="gin",
>         ),)
>
>     @classmethod
>     def __declare_last__(cls):
>         event.listen(
>             cls,
>             "after_create",
>             cls._trigger_ddl.execute_if(dialect="postgresql"),
>         )
> ```

ignore my previous email, this event handler should be failing,
there's no after_create event for a mapped class.     you'd want to
put this on the Table for which the class is mapped.    You don't need
to use __declare_last__ as the Table can take these within the
arguments:

class MyClass:
    @declared_attr
    def __table_args__(cls):
       return (Index(...), {"listeners": [("after_create", my_create_event)]})



>
> When __abstract__ = True the subclass is unmapped and no ddl is emitted on
> create_all(). I did not expect that.
>
> Is it possible to have the Mixin execute the event.listen or otherwise 
> created the trigger?
>
> Thanks,
> Luke
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> ---
> 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 https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to