1

I have updated some fields of a table in my database by removing a field and adding some, as well as adding a new table. I run:

python manage.py makemigrations
python manage.py migrate

And I get no changes detected and no changes were applied.

In django admin I don't see the new table and when I click add a new record into the database it displays the old fields. This is also reflected in the database as it displays the old fields and not the new table.

I have dropped all tables in the database and deleted all migrations in each app leaving init.py and I still get this error which confuses me because how can it show old fields which I have deleted and there are no record of as I have deleted the table and migrations?

The code in question:

from __future__ import unicode_literals
from django.db import models
from src.profiles.models import Profiles

# Create your models here.
class Product(models.Model):
    name = models.CharField(max_length=200, primary_key=True)
    description = models.CharField(max_length=1000)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

class ProductsOwned(models.Model):
    ownedID = models.ForeignKey(Profiles.user)
    product = models.ForeignKey(Product.name)
    purchaseDate = models.DateTimeField(auto_now=True)
    expiryDate = models.DateTimeField()

    def __str__(self):
        return self.name

If you need any clarification or have any questions please ask.

Edit: I can literally delete all my code in the models and no changes will still be detected.

Edit 2: Make migrations code uploaded to a pastebin: http://pastebin.com/GywuiCZj

Edit 3: The app I am editing is catalog, my installed apps:

INSTALLED_APPS = [
    'profiles',
    'contact',
    'crispy_forms',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'stripe',
    'checkout',
    'catalog',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
OultimoCoder
  • 244
  • 2
  • 7
  • 24
  • So are you getting an error when running `makemigrations`? Also, your foreign keys should be like this: `models.ForeignKey(Product)` and `models.ForeignKey(Profiles)`. (assuming Profiles is a model) – kaveh Mar 26 '17 at 20:57
  • I am new to django so thanks! I get no error whatsoever, when I drop tables and delete migrations it recreates the old database. And from then on no changes are detected. – OultimoCoder Mar 26 '17 at 21:02
  • So was that the problem? Did it fix the migration issue? – kaveh Mar 26 '17 at 21:03
  • Accidentally pressed enter I have edited my previous comment. It can't fix the migration issue as it won't detect changes. I applied what you said and it doesn't detect any changes still. – OultimoCoder Mar 26 '17 at 21:04
  • Weird! You said you deleted all migrations and dropped all the tables and still your django app created the tables based on your old models. Can you post the migrations the app creates after running `makemigrations`? – kaveh Mar 26 '17 at 21:10
  • Is the app in your `INSTALLED_APPS`? – kaveh Mar 26 '17 at 21:12
  • Yes it is in my installed apps. I have edited my post with a pastebin of makemigrations. – OultimoCoder Mar 26 '17 at 21:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139100/discussion-between-kaveh-and-oultimocoder). – kaveh Mar 26 '17 at 21:15
  • You can use a second parameter in the `makemigrations` command. The name of the app. so you can write `makemigrations catalog`. Try that. – Marcus Lind Mar 27 '17 at 02:12
  • Did you remember to touch wsgi.py? Also, does your site use a separate database on localhost than on your server? I've run into that problem before. – kloddant Mar 27 '17 at 20:39

2 Answers2

1

If your catalog does not have any data and it is safe to remove the tables related to catalog app then you can do the following.

  1. If you are using MySQL, DELETE FROM django_migrations where app = 'catalog'
  2. Delete all migrations from catalog\migrations.
  3. Now do python manage.py makemigrations catalog.

On a side note, the standard way of listing apps in settings.py is that django apps first, third party apps second and your own app third.

r0xette
  • 898
  • 3
  • 11
  • 24
  • Why is Django like this, thank you so much. The `django_migrations` totally went over my head. I'm so used to alembic using just one key value. – YetAnotherDuck Oct 31 '22 at 21:13
0

Could be possible the migration file is flagged as migrated due to certain reason.

Note: 1. If this is the 1 migration file in this app, the name of file would be 0001_initial. It could be possible you had migration file with name earlier which you could have deleted.

  1. Also something I would suggest is in installed_apps keep all the django app i.e django.contrib.admin, django.contrib.auth ... above your custom apps.

Finally If nothing works you can unfake your migration file.

python manage.py migrate --fake core 0003

python manage.py migrate --fake core zero (caution with this. It would unfake all the migration files)

and then

Reference: fake/unfake migration

Community
  • 1
  • 1
Prashant Nair
  • 306
  • 3
  • 9