Andriy
Andriy

Reputation: 1610

Is it possible to set the constraint unique = True except for empty string into SQLAlchemy?

I use the following column in my table:

email = Column(String(60), unique=True)

However, I need the possibility to save empty strings in this column. Namely, I need something like this:

email = Column(String(60), unique=True, blank=True)

but it doesn't work. Is it possible to implement this within SQLAlchemy? What is correspondent SQL (Postgresql dialect)?

Upvotes: 2

Views: 1463

Answers (1)

Stanislav
Stanislav

Reputation: 28106

You have to try to set not an empty string, but a NULL istead. As it's said in PostgreSql documentation NULL-values are not equals.

Upvotes: 1

Related Questions