使用Postgres从Windows迁移到Linux后,makemigrations失败了

时间:2018-02-05 15:45:15

标签: django postgresql

我有一个小型的网络应用程序(使用Postgres),我一直在Django Windows环境中开发,它工作正常。我现在正试图将它移到Redhat 6.8并且makemigrations失败了 我已经安装了Postgres并在Linux上创建了数据库。我创建了数据库new,因为我不需要Windows上的任何旧测试数据。我移动了所有文件(.pyc文件除外)。以下是我得到的错误。它似乎正在尝试访问尚未创建的表。 (它尚未创建,因为我还没有能够运行迁移(有点鸡/蛋范例)。注意,从我读过的内容,我还删除了除 init <之外的所有迁移文件/strong> .py。我已经和它搏斗了好几个小时。非常感谢任何帮助。 谢谢。

    webtest2) [asilver@SDNAUTOS02 sdnlabs2]$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/
    utility.execute()
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/
    self.execute(*args, **cmd_options)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/
    self.check()
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/
    include_deployment_checks=include_deployment_checks,
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/
    return checks.run_checks(**kwargs)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/checks/regi
    new_errors = check(app_configs=app_configs)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/checks/urls
    return check_resolver(resolver)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/checks/urls
    return check_method()
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/urls/resolvers.p
    for pattern in self.url_patterns:
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/utils/functional
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/urls/resolvers.p
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/utils/functional
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/urls/resolvers.p
    return import_module(self.urlconf_name)
  File "/usr/local/python2.7.14/lib/python2.7/importlib/__init__.py", line 37, in import_modul
    __import__(name)
  File "/home/asilver/pythonProjects/django/sdntest2/sdnweb/sdnlabs2/sdnlabs2/urls.py", line 24
    url(r'^inventory/', include('inventory.urls')),
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/conf/urls/__init
    urlconf_module = import_module(urlconf_module)
  File "/usr/local/python2.7.14/lib/python2.7/importlib/__init__.py", line 37, in import_modul
    __import__(name)
  File "/home/asilver/pythonProjects/django/sdntest2/sdnweb/sdnlabs2/inventory/urls.py", line 3
    from . import views
  File "/home/asilver/pythonProjects/django/sdntest2/sdnweb/sdnlabs2/inventory/views.py", line
    from .filters import *
  File "/home/asilver/pythonProjects/django/sdntest2/sdnweb/sdnlabs2/inventory/filters.py", lin
    class PerfvRtrOnlyFilter(django_filters.FilterSet):
  File "/home/asilver/pythonProjects/django/sdntest2/sdnweb/sdnlabs2/inventory/filters.py", lin
    choiceList = get_unique_choice_list(partial_query_set)
  File "/home/asilver/pythonProjects/django/sdntest2/sdnweb/sdnlabs2/inventory/lib.py", line 20
    y = set(querySet)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/db/models/query.
    self._fetch_all()
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/db/models/query.
    self._result_cache = list(self._iterable_class(self))
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/db/models/query.
    for row in compiler.results_iter(chunked_fetch=self.chunked_fetch):
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/db/models/sql/co
    results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/db/models/sql/co
    raise original_exception
django.db.utils.ProgrammingError: relation "inventory_perfvrtronly" does not exist
LINE 1: SELECT "inventory_perfvrtronly"."topoName" FROM "inventory_p...
                                                        ^

(webtest2) [asilver@SDNAUTOS02 sdnlabs2]$

4 个答案:

答案 0 :(得分:0)

您的PerfvRtrOnlyFilter类正在尝试在类级别执行查询,这意味着即使首次导入该类也会运行它。这是在任何迁移可以运行之前。

您只需在方法或函数中进行任何数据库访问。

答案 1 :(得分:0)

我收到的两条建议似乎非常相似,所以我尝试了一下列出的说明。我无法超越上面的第二步。我取消注册了应用程序,然后尝试运行迁移,但这次出现了不同的错误。有什么建议吗?

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/base.py", line 327, in execute
    self.check()
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 62, in _run_checks
    issues.extend(super(Command, self)._run_checks(**kwargs))
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/local/python2.7.14/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/asilver/pythonProjects/django/sdntest2/sdnweb/sdnlabs2/sdnlabs2/urls.py", line 24, in <module>
    url(r'^inventory/', include('inventory.urls')),
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/local/python2.7.14/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/asilver/pythonProjects/django/sdntest2/sdnweb/sdnlabs2/inventory/urls.py", line 3, in <module>
    from . import views
  File "/home/asilver/pythonProjects/django/sdntest2/sdnweb/sdnlabs2/inventory/views.py", line 5, in <module>
    from .tables import DeviceInstanceTable, PerfvRtrOnlyTable, HardwareTable
  File "/home/asilver/pythonProjects/django/sdntest2/sdnweb/sdnlabs2/inventory/tables.py", line 3, in <module>
    from .models import DeviceInstance, PerfvRtrOnly, Hardware
  File "/home/asilver/pythonProjects/django/sdntest2/sdnweb/sdnlabs2/inventory/models.py", line 8, in <module>
    class Hardware(models.Model):
  File "/home/asilver/.virtualenvs/webtest2/lib/python2.7/site-packages/django/db/models/base.py", line 118, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class inventory.models.Hardware doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

以下是该类的代码:

class PerfvRtrOnlyFilter(django_filters.FilterSet):

    deviceName = django_filters.ModelMultipleChoiceFilter(name='deviceName', label='Device', queryset=DeviceInstance.objects.all())
    hardware = django_filters.ModelMultipleChoiceFilter(name='hardware', label='Hardware', queryset=Hardware.objects.all())
    jdmVersion = django_filters.ModelMultipleChoiceFilter(name='jdmVersion', label='JDM Version', queryset=Software.objects.all().filter(component__function='hypervisor'))
    vrtrVersion = django_filters.ModelMultipleChoiceFilter(name='vrtrVersion', label='vRTR Version', queryset=Software.objects.filter(component__function='vrtr'))
    vrtr = django_filters.ModelMultipleChoiceFilter(name='vrtr', label='vRouter', queryset=Component.objects.filter(function='vrtr'))
    jflow = django_filters.BooleanFilter(name='jflow', label='Jflow')

    ##################################################
    # Mulitple Choice Filter for topoName
    ##################################################
    partial_query_set = PerfvRtrOnly.objects.values_list('topoName')
    choiceList = get_unique_choice_list(partial_query_set)
    topoName = django_filters.MultipleChoiceFilter(choices=choiceList, label='Topology')

    ##################################################
    # Mulitple Choice Filter for frameSize
    ##################################################
    partial_query_set = PerfvRtrOnly.objects.values_list('frameSize')
    choiceList = get_unique_choice_list(partial_query_set)
    frameSize = django_filters.MultipleChoiceFilter(choices=choiceList, label='Frame Size')

    ##################################################
    # Mulitple Choice Filter for Run Date
    ##################################################
    partial_query_set = PerfvRtrOnly.objects.values_list('runDate')
    choiceList = get_unique_choice_list(partial_query_set)
    runDate = django_filters.MultipleChoiceFilter(choices=choiceList, label='Run Date')

    ##################################################
    # Single Choice Filter for Bridging Technology
    ##################################################
    partial_query_set = PerfvRtrOnly.objects.values_list('bridgeTech')
    choiceList = get_unique_choice_list(partial_query_set)
    bridgeTech = django_filters.ChoiceFilter(choices=choiceList, label='Bridge Tech')

    ##################################################
    # Mulitple Choice Filter for Traffic Type
    ##################################################
    partial_query_set = PerfvRtrOnly.objects.values_list('trafficType')
    choiceList = get_unique_choice_list(partial_query_set)
    trafficType = django_filters.MultipleChoiceFilter(choices=choiceList, label='Traffic Type')

    class Meta:
        model = PerfvRtrOnly
        fields = ['qos', 'officialRun',]

以下是该功能的代码&#34; get_unique_choice_list&#34;它驻留在文件lib.py:

#####################################################################################
# This function is used to create a choice list for a filter in filter.py
# querySet is a querySet object and it should only contain one field of the table
# There may be duplicate entries in that list and each item in the list is a tupple.
#####################################################################################
def get_unique_choice_list(querySet):

    list_1= []
    y = set(querySet)
    for i in y:
        list_1.append(i)

    ######################################################################################################
    # At this point we have a list of tuples which is no good.  Ultiimately we need a tuple of tuples...
    # As show below in the example. An enumerated tuple
    # If we print list_1 we have this at this point:
    #In [38]: print list_1
    # [(u'J1101',), (u'D1001',), (u'H1101',), (u'D1',), (u'G1',), (u'G1001',)]
    ######################################################################################################
        list_2 = []
    for i in list_1:
        a = i[0]
        list_2.append(a)

    list_2.sort()
    #######################################################################
    # At this point we should have a list of topologies and no more tuples
    # print list_2
    # [u'J1101', u'D1001', u'H1101', u'D1', u'G1', u'G1001']
    #######################################################################


    #######################################################################
    # We now need to create a list of tuples
    #######################################################################
    list_3 = []
    for i in list_2:
        new_item = (str(i),str(i))
        list_3.append(new_item)

    return list_3

答案 2 :(得分:0)

最后得到了这个,但需要蛮力才能做到这一点。我删除了django应用程序中的所有文件,并启动了一个新项目和应用程序。然后我添加了models.py文件并更新了settings.py文件。那时我运行了makemigrations,一切正常。最后,我将其余的文件重新添加到项目中,现在一切正常...至少现在。我们将看到下次更改模型时会发生什么,并再次发出makemigrations命令....

答案 3 :(得分:-1)

尝试:

  • 从settings.py
  • 取消注册您的应用
  • 运行python manage.py migrate
  • 注册您的应用
  • 从您的应用中删除(保留备份)迁移文件夹
  • 运行python manage.py makemigrations
  • 比python manage.py migrate