Exclude constraint where clause not rendered when __table__.tometadata() used

Issue #3900 resolved
Chris Withers
created an issue

Given this model:

class MyModel(Base):
    __tablename__ = 'my_model'
    id = Column(Integer, primary_key=True)
    period = Column(TSRANGE())
    label = Column(String)
    __table_args__ = [ExcludeConstraint(('period', '&&'), where='label is null')]

...I would expect the following constraint to be created:

EXCLUDE USING gist (period WITH &&) where (label is null)

...but instead I get:

EXCLUDE USING gist (period WITH &&)

Comments (10)

  1. Michael Bayer repo owner

    cant reproduce:

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.dialects.postgresql import TSRANGE, ExcludeConstraint
    
    Base = declarative_base()
    
    
    class MyModel(Base):
        __tablename__ = 'my_model'
        id = Column(Integer, primary_key=True)
        period = Column(TSRANGE())
        label = Column(String)
        __table_args__ = (
            ExcludeConstraint(('period', '&&'), where='label is null'),
        )
    
    
    e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
    
    c = e.connect()
    
    t = c.begin()
    
    Base.metadata.create_all(c)
    

    output:

    CREATE TABLE my_model (
        id SERIAL NOT NULL, 
        period TSRANGE, 
        label VARCHAR, 
        PRIMARY KEY (id), 
        EXCLUDE USING gist (period WITH &&) WHERE (label is null)
    )
    
  2. Chris Withers reporter

    <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> </head> <body bgcolor="#FFFFFF" text="#000000"> <p>Which version of sqlalchemy were you using? I'll try the equivalent version here and see if I can reproduce.<br> </p> <br> <div class="moz-cite-prefix">On 27/01/2017 15:39, Michael Bayer wrote:<br> </div> <blockquote

  3. Chris Withers reporter
    • changed status to open

    Script to reproduce:

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.dialects.postgresql import TSRANGE, ExcludeConstraint
    
    Base = declarative_base()
    
    
    class MyModel(Base):
        __tablename__ = 'my_model'
        id = Column(Integer, primary_key=True)
        period = Column(TSRANGE())
        label = Column(String)
        __table_args__ = (
            ExcludeConstraint(('period', '&&'), where='label is null'),
        )
    
    
    e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
    
    c = e.connect()
    
    t = c.begin()
    
    if __name__=='__main__':
        metadata2 = MetaData()
        MyModel.__table__.tometadata(metadata2)
        metadata2.create_all(c)
    
  4. Michael Bayer repo owner

    Copy whereclause / using in ExcludeConstraint

    Fixed bug in Postgresql :class:.ExcludeConstraint where the "whereclause" and "using" parameters would not be copied during an operation like :meth:.Table.tometadata.

    Change-Id: I2f704981d4d4862f9c82a50272006fab8becebb6 Fixes: #3900

    → <<cset 1c578a710f14>>

  5. Log in to comment