1

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

2

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

Check Constraint docs for completeness

answered Apr 11, 2013 at 19:07
2
  • Docs aren't very clear here - what would the actual code look like which answers the posted question? Commented 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 constraint Commented Dec 5, 2022 at 22:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.