here is the correct way to construct and append the constraint:

class Sites(Base):
    __tablename__ = 'locations'

    site_id = Column(Integer, primary_key=True)
    site_name = Column(String(16), nullable=False)
    data_type = Column(String(12), nullable=False)
    source = Column(String(64))
    lat = Column(String(9))
    lon = Column(String(9))
    stream = Column(String(32))
    basin = Column(String(32))
    comment = Column(String)

Sites.__table__.append_constraint(
    CheckConstraint(Sites.data_type.in_(['Biogical', 'Chemical',
'Microbial', 'Physical', 'Multiple']))
)





On Sun, May 6, 2018 at 11:33 AM, Rich Shepard <rshep...@appl-ecosys.com> wrote:
> On Fri, 4 May 2018, Mike Bayer wrote:
>
>> you're looking for a table-level check constraint with IN:
>> table.append_constraint(
>>   CheckConstraint(table.c.data_type.in_('A', 'B', 'C'))
>> )
>
>
> Mike,
>
>   I'm missing how to properly use the above in my models.py module.
>
>   For example:
>
> class Sites(Base):
>     __tablename__ = 'locations'
>
>     site_id = Column(Integer, primary_key=True)
>     site_name = Column(String(16), nullable=False)
>     data_type = Column(String(12), nullable=False)
>     source = Column(String(64))
>     lat = Column(String(9))
>     lon = Column(String(9))
>     stream = Column(String(32))
>     basin = Column(String(32))
>     comment = Column(String)
>
>     locations.append.constraint(
>         CheckConstraint(locations.data_type_in('Biogical', 'Chemical',
> 'Microbial', 'Physical', 'Multiple'))
>         )
>
>   Python shows me this error:
>
> Traceback (most recent call last):
>   File "./openEDMS.py", line 18, in <module>
>     import models
>   File "/home/rshepard/development/openEDMS/models.py", line 20, in <module>
>     class Sites(Base):
>   File "/home/rshepard/development/openEDMS/models.py", line 37, in Sites
>     CheckConstraint(locations.data_type_in('Biogical', 'Chemical',
> 'Microbial', 'Physical', 'Multiple'))
> NameError: name 'locations' is not defined
>
>   If I change the table-level constraint from the tablename (locations) to
> the class name (Sites) python gives me the equivalent NameError. What syntax
> error have I made here?
>
> Regards,
>
>
> Rich
>
> --
> 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