you can use a hook like __declare_first__ to make this happen:

class AuditMixin(object):
    """created_at, updated_at, table audit mixin."""

    @classmethod
    def __declare_first__(cls):
        table = getattr(cls, '__table__', None)

        if table is not None:
            event.listen(
                table, 'after_create',
                CreateUpdateAtTrigger(table.name))

            event.listen(
                table, 'before_drop',
                DropUpdateAtTrigger(table.name))

    created_at = Column(
        TIMESTAMP(timezone=True), server_default=utcnow(), nullable=False
    )
    updated_at = Column(
        TIMESTAMP(timezone=True), server_default=utcnow(),
        server_onupdate=FetchedValue(for_update=True), nullable=False
    )



however, don't go insane like I just did, and remember to call
configure_mappers() before you try to create tables, which will
trigger hooks like __declare_first__().

There are a lot of other hooks to set these triggers up but since you
are linking to a mixin, __declare_first__ is the simplest, but needs
the configure_mappers() step to happen for it to be called.

also your function recipe needs a @compiles, I'm assuming you got that
from the documentation example.




On Tue, Oct 31, 2017 at 10:49 PM, Philip Martin
<philip.martin2...@gmail.com> wrote:
> I was trying to embed the reference code, but here is the link to the gist I
> have so far.
>
> On Tuesday, October 31, 2017 at 7:46:30 PM UTC-7, Philip Martin wrote:
>>
>>
>> I am attempting to create an audit mixin object that would use a server
>> side column trigger to set the updated_at timestamp. Ideally, whenever a
>> class inherits from a declarative base and this mixin class, the trigger
>> statement would be created after the table is created and dropped prior to
>> the table being dropped.
>>
>> So far, I have my column mixin class, AuditMixin, and I also have added
>> classes CreateUpdateAtTrigger and DropUpdateAtTrigger to compile the trigger
>> DDL create and drop statements. From here, I am a bit unsure how to proceed
>> to mount an event to any table's that use this mixin. Any help for specific
>> reference examples would be appreciated. Thanks.
>>
>>
>> <script
>> src="https://gist.github.com/pmart123/b6681e9d9c6ddc92582143e13c1c6941.js";></script>
>
> --
> 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.

Reply via email to