Yep, much nicer:

def add_constraints(mapper, class_):
    table = class_.__table__
    elements = [('period', '&&')]
    for col in table.primary_key.columns:
        if col.name!='period':
            elements.append((col, '='))
    table.append_constraint(ExcludeConstraint(*elements))
    table.append_constraint(CheckConstraint("period != 'empty'::tsrange"))

listen(Temporal, 'instrument_class', add_constraints, propagate=True)

Where's that propagate option documented?

Chris

On 24/05/2013 15:17, Michael Bayer wrote:
I would apply a single event listener to Temporal, and do all the work of 
adding the constraints and all that in the one event handler.  I'd skip 
__table_args__.

If you're on 0.8, you can apply listeners to mixins and unmapped classes using 
"propagate=True", and the events should trigger for all subclasses.


On May 23, 2013, at 7:49 AM, Chris Withers <[email protected]> wrote:

Hi All,

I have a mixin defined like this:

def add_exclude_constraint(mapper, class_):
  table = class_.__table__
  elements = [('period', '&&')]
  for col in table.primary_key.columns:
      if col.name!='period':
          elements.append((col, '='))
  table.append_constraint(ExcludeConstraint(*elements))

class Temporal(object):

  @declared_attr
  def __table_args__(cls):
      listen(cls, 'instrument_class', add_exclude_constraint)
      return (
      CheckConstraint("period != 'empty'::tsrange"),
      )

  period = Column(DateTimeRange(), nullable=False, primary_key=True)

That listen call is the source of the dirty feeling...

What's the "right" way to do this?

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
          - http://www.simplistix.co.uk

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




--
Simplistix - Content Management, Batch Processing & Python Consulting
           - http://www.simplistix.co.uk

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to