I am using postgreSQL with Django 1.10 and python 3.4. I have a Course model defined like this:
class Course(models.Model):
title = models.CharField(max_length=200, unique=True)
content = models.TextField(max_length=200)
Then i manually added unique index to the title column using the following command.
CREATE UNIQUE INDEX title_unique on wiki_course (LOWER(title));
Lets say database already has "Programming Basics" as title. When I add "programming basics" to title and hit save , it shows me the following error.
IntegrityError at /admin/wiki/course/add/
duplicate key value violates unique constraint "title_unique"
DETAIL: Key (lower(title::text))=(programming basics) already exists.
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/wiki/course/add/
Django Version: 1.10.5
Exception Type: IntegrityError
Exception Value:
duplicate key value violates unique constraint "title_unique"
On the otherhand if i switch the database from MySQL and try again it would tell me Course already exists.
Is there any way to achieve this behavior in PostgreSQL?
Update: One solution is to use to use citext type field for the title. To do this first you have to enable citext extension, using the following command.
CREATE EXTENSION IF NOT EXISTS citext;
Then use alter statement to change the type of the desired column.
ALTER TABLE course ALTER COLUMN title TYPE citext;
After executing these two queries. Django shows error in the form instead of throwing an IntegrityError exception.
If someone knows a better solution to it pls post it.

unique=Trueinside your field oftitle, so if you has duplicatestitlewith same name, it should return error..