15

This is my models.py:

class Notification(models.Model):
    user = models.ForeignKey(User)
    createdAt = models.DateTimeField(auto_now_add=True, blank=True)
    read = models.BooleanField(default=False, blank=True)

    class Meta:
        abstract = True

class RegularNotification(Notification):
    message = models.CharField(max_length=150)
    link = models.CharField(max_length=100)

class FNotification(Notification):
    # same as Notification
    pass

When I do python manage.py makemigrations, this is what it says:

Migrations for 'CApp':
  0019_auto_20151202_2228.py:
    - Create model RegularNotification
    - Create model FNotification
    - Remove field user from notification
    - Add field f_request to userextended
    - Delete model Notification

First, it's weird that it says Remove field user from notification because user is still in my Notiication model (so if anyone can figure out why it says that it say 'removing the field user from notification', that would be great!) but nonetheless, when I move on and try to do python manage.py migrate I get this message:

Applying CMApp.0019_auto_20151202_2228... OK
The following content types are stale and need to be deleted:

    CApp | notification

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.

    Type 'yes' to continue, or 'no' to cancel: no

I typed no. But what exactly does this mean, why am I getting this message and how do I make it so that I don't require this message?

SilentDev
  • 20,997
  • 28
  • 111
  • 214
  • 1
    I got this because I forgot to pull the last commit of the repository. My latest local migration was not the latest in the repository. :P I pulled the remote code, I performed the migrations again and I worked like a charm. – joaorodr84 Dec 28 '16 at 13:43

1 Answers1

13

The message you get is triggered when you remove/delete a model and do a migration.

In most cases, you can delete them safely. However, in some cases this might result to data loss. If other models have a foreign key to the removed model, these objects will also be deleted.

Here's the django ticket that requests to make deleting stale content types safer.

EDIT

As @x-yuri pointed, this ticket has been fixed and has been released in Django 1.11.

Rod Xavier
  • 3,983
  • 1
  • 29
  • 41
  • Oh, so because I added `Abstract=True` to the `Notification` model, that is probably why Django assumes that I deleted it. Any idea why it says `- Remove field user from notification` then? Because the `user` ForeignKey is still there, so why Does Django say that it is removed? – SilentDev Dec 03 '15 at 06:58
  • I'm not entirely sure about that. Maybe you can add the migration file so we can examine what's happening. – Rod Xavier Dec 03 '15 at 23:49
  • @RodXavier in my case I have a model A that has been deleted with foreign keys to model B, C. It means that by doing the migration, the data in model B,C will be deleted or only if there is a foreign key to model A? Thanks – Pietro Jan 15 '17 at 10:31
  • @Pietro, data in model B,C won't be deleted if you delete the stale data from table A. – Rod Xavier Jan 16 '17 at 03:28
  • The issue in the ticket has been fixed, and the changes (plus [some more](https://code.djangoproject.com/ticket/24865)) has been released in [Django 1.11](https://docs.djangoproject.com/en/2.0/releases/1.11/#django-contrib-contenttypes). – x-yuri Feb 07 '18 at 22:55
  • Thanks @x-yuri, I updated the answer to include your comment. – Rod Xavier Feb 07 '18 at 23:59