I am trying to specify this check constraint on a column via SQLAlchemy.
delete_flag char(1) not null CHECK(delete_flag in('Y','N')),
I am not able to figure out the syntax to achieve this. Any link, tutorial, advise will be of great help.
Thanks Tara Singh
Daniel Kluev
11.4k2 gold badges38 silver badges36 bronze badges
asked Jul 29, 2010 at 20:31
1 Answer 1
You should be using the SQLAlchemy Enum type in this case.
import enum
from sqlalchemy import Enum
class YesNo(enum.Enum):
yes = 'Y'
no = 'N'
t = Table(
'data', MetaData(),
Column('value', Enum(YesNo, create_constraint=True))
)
connection.execute(t.insert(), {"value": YesNo.no})
assert connection.scalar(t.select()) is YesNo.no
answered Apr 11, 2013 at 19:07
-
Docs aren't very clear here - what would the actual code look like which answers the posted question?baxx– baxx2022年11月15日 17:11:06 +00:00Commented Nov 15, 2022 at 17:11
-
I've added the example, but it's literally the example from the docs with "one = 1, two = 2, three = 3" replaced with "yes = 'Y', no = 'N'" and create_constraint=True to create the constraintMark Harviston– Mark Harviston2022年12月05日 22:04:40 +00:00Commented Dec 5, 2022 at 22:04
lang-py