Why does Django keeps asking content types are stale and need to be deleted

余生长醉 提交于 2019-12-13 12:29:58

问题


I've tried everything found:

Can stale content types be automatically deleted in Django?

Deleting unused Models, stale content types prompt

InvalidBasesError: Cannot resolve bases for [<ModelState: 'users.GroupProxy'>]

Django Wagtail CMS migrate: Cannot resolve bases for [<ModelState: 'app.CustomPage'>

Django migrate with zinnia- InvalidBasesError: Cannot resolve bases for [<ModelState: 'zinnia.Author'>]

So here's my problem: I have:

  • a ComicBook that has a many-to-many Planche's
  • a Planche that has a many-to-many Bande's
  • a Bande that has a many-to-many Vignette's
  • ... and three levels deeper (it's not important it's always the same principle)

I needed in-between many-to-many tables to add "importance" field to be able to make a custom sort of the relationships. Thus I've created

  • a ComicBookPlanche that is the many-to-many table with field importance
  • a PlancheBande that is the many-to-many table with field importance

Everything was working perfectly until I decide to rename ComicBook to Book. From now on I always get the message django.db.migrations.state.InvalidBasesError: Cannot resolve bases for...

I even tried to drop all the tables and migration folder, nothing changed... I tried to comment my application -> great then un-comment and still:

django.db.migrations.state.InvalidBasesError:
Cannot resolve bases for
[<ModelState: 'main.TexteLongTextesThrough'>,
 <ModelState: 'main.TexteCourtTextesThrough'>,
 <ModelState: 'main.VignetteBullesThrough'>,
 <ModelState: 'main.LivrePlanchesThrough'>]

I'm getting mad. So here's what I did:

  • brand new application
  • makemigrations then migrate -> auth, admin, sessions, sites created no problem
  • copy/paste my models.py without admin.py.

makemigrations -> perfect:

Migrations for 'main':
  0001_initial.py:
    - Create model Bande
    - Create model BandeVignette
    - Create model Bulle
    - Create model ContenuCourt
    - Create model ContenuLong
    - Create model Langue
    - Create model Livre
    - Create model Personne
    - Create model Planche
    - Create model PlancheBande
    - Create model TexteCourt
    - Create model TexteLong
    - Create model Vignette
    - Add field description to planche
    - Add field planches to livre

Then migrate -> perfect:

Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: sessions, admin, sites, auth, contenttypes, main
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying main.0001_initial... OK

Process finished with exit code 0

Then copy/paste my admin.py then makemigrations -> perfect:

Migrations for 'main':
  0002_livreplanchesthrough_textecourttextesthrough_textelongtextesthrough_vignettebullesthrough.py:
    - Create proxy model LivrePlanchesThrough
    - Create proxy model TexteCourtTextesThrough
    - Create proxy model TexteLongTextesThrough
    - Create proxy model VignetteBullesThrough

Process finished with exit code 0

Then each time I try migrate it keeps asking me this, no matter if I anwser "yes" or "no":

>>> migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: sessions, admin, sites, auth, contenttypes, main
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.
The following content types are stale and need to be deleted:

    main | textelong_textes
    main | textecourt_textes
    main | livre_planches
    main | vignette_bulles

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:  yes
Process finished with exit code 0

What can I do to make him stop asking, and what is the problem?


回答1:


A few things here: It looks like you created the models in one batch of migrations, and then created the through tables in a second batch of migrations. This is wrong, you should have the through tables written and migrated at the same time of the main models.

What happened in the last example is that when you first created the models, django went and created it's own standard through tables, then you went and added custom through tables, so django is asking you to delete the original (old) ones.

The way you've worded everything, it looks like you placed the model definitions for the through tables in admin.py? Why would you do that? They should be in models.py right next to the models they are "connecting".

Also, you shouldn't be using a Proxy model, and without the actual source code, that may very well be the root cause of your problem. If all you're trying to do is have an extra field on the through relationship, you should follow the pattern here: https://docs.djangoproject.com/en/1.8/topics/db/models/#extra-fields-on-many-to-many-relationships



来源:https://stackoverflow.com/questions/33998660/why-does-django-keeps-asking-content-types-are-stale-and-need-to-be-deleted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!