我已尝试py manage.py makemigrations
然后py manage.py migrate
但是当我向模型添加新字段时,会发生此错误。我也尝试删除所有迁移并执行makemigrations并再次迁移,但它仍然无法解决问题。
基本上我有一个模型Schedule
,我在其中添加了Customer
字段,其关系为customer = models.ForeignKey(Customer)
这是我的模特:
class Schedule(models.Model):
name = models.CharField(max_length=250)
deadline_date = models.DateField()
is_completed = models.BooleanField(default=False)
description = models.CharField(max_length=1000, default="")
customer = models.ForeignKey(Customer)
以下是迁移:
class Migration(migrations.Migration):
initial = True
dependencies = [
('core', '0007_customer'),
]
operations = [
migrations.CreateModel(
name='Schedule',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=250)),
('deadline_date', models.DateField()),
('deadline_time', models.TimeField()),
('is_completed', models.BooleanField(default=False)),
('description', models.CharField(default='', max_length=1000)),
('responsible_department', models.CharField(choices=[('Inventory', 'Inventory'), ('Sales', 'Sales'), ('Procurement', 'Procurement'), ('Accounting', 'Accounting'), ('Technical', 'Technical'), ('Admin', 'Admin')], default='Sales', max_length=30)),
('customer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.Customer')),
],
),
]
这是追溯:
Traceback:
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\backends\utils.py" in execute
65. return self.cursor.execute(sql, params)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\backends\sqlite3\base.py" in execute
328. return Database.Cursor.execute(self, query, params)
The above exception (no such column: schedule_schedule.customer_id) was the direct cause of the following exception:
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\contrib\admin\options.py" in wrapper
551. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\utils\decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\views\decorators\cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\contrib\admin\sites.py" in inner
224. return view(request, *args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\utils\decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\utils\decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\utils\decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\contrib\admin\options.py" in changelist_view
1662. selection_note=_('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\models\query.py" in __len__
232. self._fetch_all()
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\models\query.py" in _fetch_all
1118. self._result_cache = list(self._iterable_class(self))
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\models\query.py" in __iter__
53. results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\models\sql\compiler.py" in execute_sql
894. raise original_exception
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\models\sql\compiler.py" in execute_sql
884. cursor.execute(sql, params)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\backends\utils.py" in execute
80. return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\backends\utils.py" in execute
65. return self.cursor.execute(sql, params)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\utils\six.py" in reraise
685. raise value.with_traceback(tb)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\backends\utils.py" in execute
65. return self.cursor.execute(sql, params)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\backends\sqlite3\base.py" in execute
328. return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /admin/schedule/schedule/
Exception Value: no such column: schedule_schedule.customer_id
答案 0 :(得分:0)
如果您删除了所有迁移并重新运行makemigrations
,那么您的迁移和数据库将不同步。
最简单的解决方法是删除数据库并再次运行migrate
。如果您拥有重要数据并且无法删除数据库,那么让数据库和迁移重新同步可能会非常棘手。您需要手动创建customer_id
列,并可能进行其他更改。
答案 1 :(得分:0)
你把它命名为两次。
customer = ...(Customer)
Django无法区分。
答案 2 :(得分:0)
就我而言,我打开了sqlite数据库,然后手动添加了该字段。这为我解决了问题。