This is from django's documentation:
Field.unique
If True, this field must be unique throughout the table.
This is enforced at the database level and by model validation. If you try to save a model with a duplicate value in a unique field, a django .db.IntegrityError will be raised by the model’s save() method.
Here is my models.py
class MyModel(models.Model):
# my pk is an auto-incrementing field
url = models.URLField("URL", unique=True)
text = models.TextField(max_length=1000)
# my model is just two fields, one pk (unique), and another unique field,
#, the url
Here my is manage.py sqlall (I ran syncdb)
CREATE TABLE `MyModel_mymodel` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`url` varchar(200) NOT NULL UNIQUE,
`text` varchar(1000) NOT NULL,
However, in the manage.py shell, I can freely do this:
>>> from MyModel.models import MyModel
>>> MyModel().save() # it works fine!? Not even the text was checked for!
>>> MyModel(url="blah").save()
>>> MyModel(url="blah").save() # it still works!
# I checked the mysql database afterwards, the models were saved just fine, they
# however did have different PK's (auto incrementing fields).
I'm using mysql, django 1.5. Does anyone have an idea what could possible be causing this?
I am using a custom manager, but I doubt that's the issue.
Thanks.
IntegrityError: column url is not unique
exception for the secondMyModel(url="blah").save()
. Did you paste your code as is?text
field definition has typo..modes
. I suspect you did not copy&paste your code as is.