views:

173

answers:

4

Hi,

I have a model with a CharField that earlier had "unique" set to True, but wich now is changed to False. But when i try to save that field with something that exists django still throws an IntegrityError.

How can i change this behavior so that it allows non distinct values?

The database is MySQL.

A: 

Drop the unique index.

Ignacio Vazquez-Abrams
I assume you mean to drop the unique index on the MySQL table? (just to avoid confusion for people new to Django who might think the index should be dropped in the Django model)
tomlog
That is indeed what I mean.
Ignacio Vazquez-Abrams
Ok, I tried to this: 'ALTER TABLE `tblname` DROP INDEX `fieldname`'But that didnt work. Still thows integrity error.
Espen Christensen
Are you sure that query you issued ran correctly? You should first check what are the names of indexes in your table. You can issue SHOW INDEX FROM `mytable` FROM `mydb`;against your database and it should show indexes in that table- you can then drop the unique one.
fest
That worked, I used the wrong name when I tried to remove the index, but after i listed the indexes and got the right name I managed to solve it.So after you change to unique=False do this.SHOW INDEX FROM mytable FROM mydb;ALTER TABLE mytable DROP INDEX indexname;
Espen Christensen
A: 

have you made sure that you re-synced your database in django using the "python manage.py syncdb" command at the command line before re-running your web application? This might solve your problem.

Archie1986
syncdb never touches existing tables, so this won't solve the problem.Migrations tool, like south, would.
fest
A: 

I was attempting to do the same thing, and found that as of django-south 0.5, south doesn't handle this for you.

Like was stated in the previous comments, you should probably create your south migration first, but then you will have to manually go in and delete the unique index that django initially creates when you do your first syncdb.

use mydb;
desc mytable;

That will show you the schema for the table, and you will see that the field will still have a value of "UNI" in the key column.

SHOW INDEX FROM mytable FROM mydb;

Should be an index that has Non_unique set to 0.

ALTER TABLE mytable DROP INDEX indexname;

If you look at the table schema again, you will see that "UNI" is no longer in the key column.

bfrederi
+1  A: 

I suggest using http://south.aeracode.org, it's a DB migration app for Django and it allows to make such changes without even touching MySQL shell/admin. This way you have kind of replayable macros for updating any computer to the latest DB scheme.

And using it is as simple as what they have written here: http://south.aeracode.org/wiki/ConvertingAnApp

Tomasz Zielinski