-1

So I changed the names and added a column to a model in django.

Before:

class MyApp(models.Model):
    id = models.AutoField(primary_key=True)
    aaa = models.CharField(max_length=100)
    bbb = models.CharField(max_length=100)

After:

class MyApp(models.Model):
    id = models.AutoField(primary_key=True)
    first = models.CharField(max_length=100)
    second = models.CharField(max_length=100)
    third = models.CharField(max_length=100)

I made these changes in the MyApp's view, model, and serializer, however when I try to access my new API endpoint, it fails because the database doesn't contain the new column names. How can I update the database to reflect my new model? (I don't care about any of the data for this model, so I can wipe it). No idea how to do this

  • 1
    stupid question but did you make migrations and migrated afterwards? – Alexander Tyapkov Oct 23 '17 at 20:20
  • @AlexanderTyapkov I made migrations, the migrations for the app I chose were successfully generated (I also checked, it generated the folder), but when I went to migrate it said no changes were detected so I thought it must be something else. – asdasd2a43qaad Oct 23 '17 at 20:23
  • sound like migrations where not applied. try to run migrations and specify the name of the app -> python manage.py migrate your_app_name – Alexander Tyapkov Oct 23 '17 at 20:26
  • @AlexanderTyapkov still get no migrations applied – asdasd2a43qaad Oct 23 '17 at 20:41
  • did you make migrations for this app before? – Alexander Tyapkov Oct 23 '17 at 20:43
  • @AlexanderTyapkov Yes, I have migrated, the current schema (the before in my code) has data in it and functioning. However, I need my app to have a new schema, but I don't care if I lose the data. I think I got further with the error though. It now reconizes there is something to migrate however I get `django.db.utils.OperationalError: table "MyApps_MyApp" already exists` – asdasd2a43qaad Oct 23 '17 at 20:47
  • can you post your migrations because there is not enough info about what is happening. Look like your migrations tries to create new table, but should update the old one. – Alexander Tyapkov Oct 23 '17 at 20:49
  • @AlexanderTyapkov I ended up just having to go into the database and drop the table. That fixed it – asdasd2a43qaad Oct 23 '17 at 21:03
  • great! If you don't care about data and tables then probably it is the easiest way to solve it. – Alexander Tyapkov Oct 23 '17 at 21:04

1 Answers1

1

Make sure you run the commands

python manage.py makemigrations appname

python manage.py migrate appname

If you get table already exists run command

python manage.py migrate --fake appname

This solved the problem when I had it. https://docs.djangoproject.com/en/1.11/topics/migrations/#initial-migrations

sumpen
  • 503
  • 6
  • 19
  • Wouldn't `--fake-initial` be more appropriate? https://stackoverflow.com/questions/42695629/fake-initial-vs-fake-in-django-migration – souldeux Oct 23 '17 at 23:06