On Fri, May 4, 2018 at 4:49 PM, Rich Shepard <[email protected]> wrote:
> In postgres (and I believe also in sqlite3) values in a table column can
> be restricted to certain values.
>
> In models.py the class Sites() includes this column:
>
> data_type = Column(String(12), nullable=False,
> CheckConstraint('Biogical', 'Chemical', 'Microbial',
> 'Physical',
> 'Multiple'))
>
> but Python doesn't like this syntax:
>
> Traceback (most recent call last):
> File "./openEDMS.py", line 18, in <module>
> import models
> File "/home/rshepard/development/openEDMS/models.py", line 28
> data_type = Column(String(64), nullable=False,
> CheckConstraint('Biogical',
> 'Chemical', 'Microbial',
> 'Physical', 'Multiple')) ^
> SyntaxError: positional argument follows keyword argument
>
> My web search found examples and the SA CHECK constraint description, but
> none used a list of strings as acceptable values. I need to learn how to
> implement this constraint as there are several model classes that use it.
you're looking for a table-level check constraint with IN:
table.append_constraint(
CheckConstraint(table.c.data_type.in_('A', 'B', 'C'))
)
alternatively, just use the backend-agnostic Enum type with native=False:
http://docs.sqlalchemy.org/en/latest/core/type_basics.html?highlight=enum#sqlalchemy.types.Enum
gives you the same CHECK constraint
>
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.