Django 1.7.1 迁移错误

7

在引入新的应用程序(django-allauth)后执行迁移时出现错误。我不确定还有什么其他方法可以修复错误。我尝试了一些方法,但遗憾的是它们似乎没有帮助。

运行manage.py migrate命令时:

File "D:\Python27\Lib\site-packages\django\db\migrations\state.py", line 71, 
in render raise     
InvalidBasesError("Cannot resolve bases for %r\nThis can happen if you are inheriting 
models from an app with migrations (e.g. contrib.auth)\n in an app with no migrations; 
see https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies for more" % 
new_unrendered_models)
django.db.migrations.state.InvalidBasesError: Cannot resolve bases for 
[<ModelState: 'blog.BlogPage'>, <ModelState: 'blog.BlogIndexPage'>]
This can happen if you are inheriting models from an app with migrations 
(e.g. contrib.auth) in an app with no migrations; see
https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies for more

models.py

    from django.db import models
    from wagtail.wagtailcore.models import Page, Orderable
    from wagtail.wagtailcore.fields import RichTextField
    from wagtail.wagtailadmin.edit_handlers import FieldPanel  ,MultiFieldPanel,InlinePanel, PageChooserPanel
    from modelcluster.fields import ParentalKey

class BlogPage(Page):
    body = RichTextField()
    date = models.DateField("Post date")
    indexed_fields = ('body', )
    search_name = "Blog Page"

BlogPage.content_panels = [
    FieldPanel('title', classname="full title"),
    FieldPanel('date'),
    FieldPanel('body', classname="full"),
]


class LinkFields(models.Model):
    link_page = models.ForeignKey(
        'wagtailcore.Page',
        null=True,
        blank=True,
        related_name='+'
    )

panels = [
    PageChooserPanel('link_page'),
]

class Meta:
    abstract = True

class RelatedLink(LinkFields):
    title = models.CharField(max_length=255, help_text="Link title")
    panels = [
         FieldPanel('title'),
         MultiFieldPanel(LinkFields.panels, "Link"),
     ]

     class Meta:
         abstract = True


 class BlogIndexPageRelatedLink(Orderable, RelatedLink):
     page = ParentalKey('blog.BlogIndexPage', related_name='related_links')

 class BlogIndexPage(Page):
     intro = models.CharField(max_length=256)
     indexed_fields = ('body', )
     search_name = "Blog Index Page"

 BlogIndexPage.content_panels = [
     FieldPanel('title', classname="full title"),
     FieldPanel('intro', classname="full"),
     InlinePanel(BlogIndexPage, 'related_links', label="Related links"),
 ]    

到目前为止我尝试了以下方法:

  1. 按照https://dev59.com/MYPba4cB1Zd3GeqPr2uz#25858659中的建议操作,但对我来说没有任何变化。
  2. 我还尝试了https://code.djangoproject.com/ticket/22051#comment:12,但没有成功。

注意:makemigrations运行正常(未检测到更改),但是migrate失败。

平台设置:当前在Windows系统上使用Django 1.7.1。django-allauth在此系统中的其他应用程序中也可以成功运行。

有人遇到过这种情况并且有解决办法吗?

提前致谢。

---下面是发出的指令序列:

     (env) D:\git\rebootv2.1\blog>python manage.py migrate
     D:\Python27\Lib\site-packages\treebeard\mp_tree.py:102: RemovedInDjango18Warning:      `MP_NodeManager.get_query_set` method
 should be renamed `get_queryset`.
   class MP_NodeManager(models.Manager):

 Operations to perform:
   Synchronize unmigrated apps: account, allauth, modelcluster, blog, compressor, facebook,      wagtailsnippets, socialaccount
   Apply all migrations: core, wagtailusers, wagtailembeds, wagtailadmin, sessions, admin,      wagtailcore, sites, auth, contenttypes, wagtaildocs, taggit, wagtailsearch, wagtailforms,      wagtailredirects, wagtailimages
 Synchronizing apps without migrations:
   Creating tables...
   Installing custom SQL...
   Installing indexes...
 Running migrations:
   Applying sites.0001_initial...Traceback (most recent call last):
   File "manage.py", line 10, in <module>
     execute_from_command_line(sys.argv)
   File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
utility.execute()
   File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
   File "D:\Python27\Lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
   File "D:\Python27\Lib\site-packages\django\core\management\base.py", line 338, in execute
output = self.handle(*args, **options)
   File "D:\Python27\Lib\site-packages\django\core\management\commands\migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
   File "D:\Python27\Lib\site-packages\django\db\migrations\executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
   File "D:\Python27\Lib\site-packages\django\db\migrations\executor.py", line 91, in apply_migration
if self.detect_soft_applied(migration):
   File "D:\Python27\Lib\site-packages\django\db\migrations\executor.py", line 135, in detect_soft_applied
apps = project_state.render()
   File "D:\Python27\Lib\site-packages\django\db\migrations\state.py", line 71, in render raise InvalidBasesError("Cannot resolve bases for %r\nThis can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)\n in an app with no migrations; see https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies for more" % new_unrendered_models)
 django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<ModelState: 'blog.BlogPage'>, <ModelState: 'blog.BlogIndexPage'>]
 This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
  in an app with no migrations; see https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies for more

 (env) D:\git\rebootv2.1\blog>python manage.py makemigrations
 D:\Python27\Lib\site-packages\treebeard\mp_tree.py:102: RemovedInDjango18Warning: `MP_NodeManager.get_query_set` method
 should be renamed `get_queryset`.
   class MP_NodeManager(models.Manager):

 No changes detected
[问题解决方法] - 最后发现是排序的问题吧……
  1. 在settings.py中禁用所有的allauth应用程序
  2. 运行manage.py migrate,启用所有的allauth应用程序,并禁用为项目生成的wagtail应用程序(例如博客)
  3. 再次运行manage.py migrate,启用INSTALLED_APPS中的两组应用程序
  4. 再次运行manage.py migrate

现在看起来已经解决了问题。

希望这对某些人有所帮助并节省时间!


'blog' 应用程序是否有任何迁移创建? - Kevin Cherepski
是的,问题已经解决了。manage.py migrate 已经顺利执行。我所做的修复步骤如下:在 settings.py 中禁用所有 allauth 应用程序运行 manage.py migrate启用所有 allauth 应用程序并禁用为项目生成的 wagtail 应用程序(例如博客)再次运行 manage.py migrate在 INSTALLED_APPS 中启用两组应用程序再次运行 manage.py migrate现在看起来一切都正常了。希望这能帮助某些人节省时间! - AndrewO
您可以在“Answer”中提供此详细信息....! - Raja Simon
现在已更新答案。 - AndrewO
5个回答

18

我也遇到了错误Cannot resolve bases for ... This can happen if you are inheriting models from an app with migrations。这是由于python manage.py makemigrations未创建任何迁移文件导致的。这是因为我没有migrations文件夹。添加了该文件夹(并在其中加入空的__init__.py文件)后,一切都正常了。


刚刚也用了这个修复方法来解决1.710的问题。 - Jesuisme
1
在我的情况下,这是同一类型错误的解决方案。 - Sami

13

最终似乎是一个有关排序的问题....

  1. 在settings.py中禁用所有allauth应用程序的INSTALLED_APPS。
  2. 运行manage.py migrate,启用所有allauth应用程序并禁用为项目(例如blog)生成的wagtail应用程序。
  3. 再次运行manage.py migrate,启用INSTALLED_APPS中的两组应用程序。
  4. 再次运行manage.py migrate。

现在似乎一切都很顺利。

希望这可以帮助某些人并节省他们的时间!


我做了跟你有点像的事情,对我的大部分应用进行了注释,运行了./manage.py migrate并成功地“伪造”了迁移。然后我取消对自己的应用程序的注释,再次运行了./manage.py makemigrations,这一次它检测到了变化并更新了我的迁移。我随机取消了一些“INSTALLED_APPS”并设法运行所有的迁移。我想这是因为我更新了Django和Django-CMS,也许我跳过了某些迁移,无论如何,感谢你给出的提示,现在一切正常 :) - GabLeRoux
似乎一度起作用,但我尝试添加其他应用程序后,又出现了同样的问题。https://bitbucket.org/bercab/cmsplugin-nivoslider/issue/10/migration-fails-on-latest-django - GabLeRoux
我终于找到了问题所在,cmsfiler在Django的迁移中使用自定义模块名称(migrations_django而不是migrations)。详情请参见:https://bitbucket.org/bercab/cmsplugin-nivoslider/issue/10/migration-fails-on-latest-django#comment-15395179 参考文献:https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-MIGRATION_MODULES - GabLeRoux
我还需要阻止从 urls.py 到我的应用程序的链接。 - John Pang

1

我在尝试使用wagtail 演示时遇到了这个问题(不需要尝试安装第三方应用程序)。由于错误出现在treebeard中,我猜测可能有一个更新的版本可用。果然,这解决了问题:

pip uninstall django-treebeard
pip install django-treebeard==3.0

现在,在wagtail演示设置中,我可以运行此命令而没有错误:
./manage.py load_initial_data

我今天尝试了这种方法,但似乎对我没有起作用。很高兴听到它对你有用。 - AndrewO

0
在我的情况下,这是因为我在设置“ .gitignore”期间删除了特定应用程序文件夹下的“迁移”文件夹。Wagtail需要这些表格,但无法创建。因此,在这种情况下,需要执行以下操作:
  1. 确保每个已创建应用程序文件夹(带有models.py)中都有'migrations'文件夹,并在其中添加__init__.py文件。如果没有,请在必要时创建它们。
  2. 运行python manage.py makemigrations。它将检查迁移文件夹是否具有正确的指令以创建所有必要的实际表格,并在不存在时创建0001_initial.py。
  3. 运行python manage.py migrate

0
在我的情况下,我创建了一个继承自auth.models.User的模型。
class User(auth.models.User, auth.models.PermissionsMixin):
    def __str__(self):
        return "@{}".format(self.username)

这就是错误的原因。

解决方案:

禁用(注释掉)该模型 => 运行迁移 => 启用该模型 => 再次运行迁移。应该可以解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接