4

Is there a straightforward clean way to drop a field from Django model? for addition of a field, migrate works perfect but its not working for deletion throwing the following error

django.db.utils.DatabaseError: (1060, "Duplicate column name 'xyz_json_old'")

comiventor
  • 3,922
  • 5
  • 50
  • 77

3 Answers3

10

If you want to drop only the field from the model try this

$ python manage.py dbshell 

You will get directly within your database shell (mysql or psql) it up to what database you are using.

$ mysql> | psql> ALTER TABLE <table_name> DROP column <COLUMN_NAME>;

Open the model too (the python file)and take off the field name from the model and type now

$ python manage.py syncdb

And it will drop the column to from table, doesn't matter if the table is already populated or not.

Armance
  • 5,350
  • 14
  • 57
  • 80
drabo2005
  • 1,076
  • 1
  • 10
  • 16
0

I am using south but still I had to go through the following with the help of my friend -

He says I must have botched up something and some internal steps in south are not atomic

  1. edit the models.py file and delete the column from the class
  2. BASH PROMPT$ python manage.py schemamigration core --auto
  3. MYSQL PROMPT> alter table core_tablename drop column xyz_json_old; commit;
  4. BASH PROMPT$ python manage.py core
comiventor
  • 3,922
  • 5
  • 50
  • 77
-1

You can use South.

  • Edit your settings.py and put 'south' into INSTALLED_APPS (assuming you’ve installed it to the right place)
  • Run ./manage.py syncdb to load the South table into the database. Note that syncdb looks different now - South modifies it.
  • Run ./manage.py convert_to_south myapp -South will automatically make and pretend to apply your first migration.

Then edit your models.py, and then

./manage.py schemamigration --auto myapp

them migrate:

./manage.py migrate myapp
Armance
  • 5,350
  • 14
  • 57
  • 80
atupal
  • 16,404
  • 5
  • 31
  • 42
  • I suspect he's already using South, I think he's trying to migrate and that's where he's hitting the error. He may need to --fake a migration – ptr Jan 07 '14 at 12:52
  • This answer is very outdated. – mmla Oct 10 '19 at 01:01