当前位置: 代码迷 >> 综合 >> 错误:django.db.migrations.exceptions.CircularDependencyError
  详细解决方案

错误:django.db.migrations.exceptions.CircularDependencyError

热度:33   发布时间:2024-02-10 08:30:44.0

(环境:MacBook Pro Retina; python 3.8.5; Django:3.1)

在执行migration时,发生如下错误:

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/migrations/graph.py", line 274, in ensure_not_cyclicraise CircularDependencyError(", ".join("%s.%s" % n for n in cycle))
django.db.migrations.exceptions.CircularDependencyError: imagestore.0001_initial, mystore.0001_initial

感谢:https://stackoverflow.com/questions/40705237/django-db-migrations-exceptions-circulardependencyerror解决了。

原因:与外键相关的两个应用之间的循环依赖关系错误

我的upload.py(如下)与album.py相互引用

class AlbumUpload(models.Model):zip_file = models.FileField(verbose_name=_('images file (.zip)'), upload_to=TEMP_DIR,help_text=_('Select a .zip file of images to upload into a new Gallery.'))album = models.ForeignKey(swapper.get_model_name('imagestore', 'Album'),on_delete=models.CASCADE, blank=True, null=True,help_text=_('Select an album to add these images to. leave this empty ''to create a new album from the supplied title.'))new_album_name = models.CharField(verbose_name=_('New album name'), max_length=255, blank=True,help_text=_('If not empty new album with this name will be created ''and images will be upload to this album'))

步骤一:先注释掉外键:

class AlbumUpload(models.Model):zip_file = models.FileField(verbose_name=_('images file (.zip)'), upload_to=TEMP_DIR,help_text=_('Select a .zip file of images to upload into a new Gallery.'))#album = models.ForeignKey(#swapper.get_model_name('imagestore', 'Album'),#on_delete=models.CASCADE, blank=True, null=True,#help_text=_('Select an album to add these images to. leave this empty '#            'to create a new album from the supplied title.'))new_album_name = models.CharField(verbose_name=_('New album name'), max_length=255, blank=True,help_text=_('If not empty new album with this name will be created ''and images will be upload to this album'))

步骤二:删除项目内的“migration”文件夹

步骤三:执行python manage.py makemigrations

(venv) (base) SulleyMac:testproject mac$ python manage.py makemigrations
Migrations for 'imagestore':/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/imagestore/migrations/0003_auto_20200812_1343.py- Alter field head on album- Alter field zip_file on albumupload
Migrations for 'imagestore_cms':/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/imagestore/imagestore_cms/migrations/0002_auto_20200812_1343.py- Alter field cmsplugin_ptr on imagestorealbumcarousel- Alter field cmsplugin_ptr on imagestorealbumptr

步骤四:执行python manage.py migrate

(venv) (base) SulleyMac:testproject mac$ python manage.py migrate
Operations to perform:Apply all migrations: admin, auth, cms, contenttypes, imagestore, imagestore_cms, menus, sessions, sites, tagging, thumbnail
Running migrations:Applying imagestore.0003_auto_20200812_1343... OKApplying imagestore_cms.0002_auto_20200812_1343... OK

解决。

  相关解决方案