Django Migrations不会在模型中应用更改

时间:2018-06-15 15:05:35

标签: django django-models

每当我在models.py和以下命令中添加/删除/修改任何内容时,项目都不会对数据库模型进行更改。

python manage.py makemigrations

OR

python manage.py makemigrations

OR

Python manage.py migrate

以上所有命令都会抛出同样的错误: " return self.cursor.execute(sql,params) django.db.utils.ProgrammingError:列 "

尝试删除迁移文件夹(不删除,运行虚假迁移,运行零迁移但没有任何帮助。

请告知。

保存Models.py文件并包含:

class studentmaster(models.Model):

studentid = models.IntegerField(primary_key=True)
studentname = models.CharField(max_length=30)
isstaff= models.BooleanField(default=False)

运行迁移时,下面提供了完整的跟踪:

python manage.py migrate --fake
Traceback (most recent call last):
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column loginapp_studentmaster.isstaff does not exist
LINE 1: ...udentid", "loginapp_studentmaster"."studentname", "loginapp_...
                                                             ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/core/management/base.py", line 332, in execute
    self.check()
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 58, in _run_checks
    issues.extend(super()._run_checks(**kwargs))
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/core/management/base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/urls/resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/urls/resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/ankit/smartswypetest/smartswypetest/UserLogin/urls.py", line 18, in <module>
    from loginapp import views
  File "/home/ankit/smartswypetest/smartswypetest/loginapp/views.py", line 10, in <module>
    from .forms import *
  File "/home/ankit/smartswypetest/smartswypetest/loginapp/forms.py", line 32, in <module>
    class AnnForm(forms.Form):
  File "/home/ankit/smartswypetest/smartswypetest/loginapp/forms.py", line 42, in AnnForm
    choices=[(str(s.studentid), s.studentname)for s in studentmaster.objects.all()],
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __iter__
    self._fetch_all()
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1066, in execute_sql
    cursor.execute(sql, params)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/ankit/miniconda3/envs/sswypeenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column loginapp_studentmaster.isstaff does not exist
LINE 1: ...udentid", "loginapp_studentmaster"."studentname", "loginapp_...

1 个答案:

答案 0 :(得分:0)

您的AnnForm正在类定义中进行查询,该定义在迁移可以运行之前的导入时执行。

你不应该写这样的表格。将ModelChoiceField与查询集一起使用。