Re: [sqlalchemy] Refactoring events / triggers into Mixins
It was an error from my hardcoded ddl statement not matching the resulting Class.__tablename__. So the error was from the database. No issues with sqlalchemy at all. The resulting Mixin does what I expect. Thanks again for your incredible dedication on this listserv and with open source. - 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Refactoring events / triggers into Mixins
On Wed, Nov 7, 2018 at 9:51 PM Luke wrote: >> >> 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)]}) > > > That seems to be working. I've got a new error that tells me I need to insert > the __tablename__ of > the subclass into my DDL statement. Does SqlAlchemy have a way to do this? i have no idea what this error is. can you please copy a complete stack trace and the error itself? > > - 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 sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Refactoring events / triggers into Mixins
It's as simple as cls.__name__ isn't it? Thanks again. - Luke PS: Some people on this list may find my (very alpha) pytest-pgtap library helpful: https://github.com/lmergner/pytest-pgtap -- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Refactoring events / triggers into Mixins
> > 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)]}) > That seems to be working. I've got a new error that tells me I need to insert the __tablename__ of the subclass into my DDL statement. Does SqlAlchemy have a way to do this? - 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Refactoring events / triggers into Mixins
On Wed, Nov 7, 2018 at 7:34 PM Luke 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 sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Refactoring events / triggers into Mixins
You need to put propagate=True on your listen call for it to take effect for subclasses. On Wed, Nov 7, 2018, 7:34 PM Luke 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"), > ) > ``` > > 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 sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Refactoring events / triggers into Mixins
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"), ) ``` 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.