JM Lontoc
JM Lontoc

Reputation: 221

OperationalError no such column

I have tried py manage.py makemigrations and then py manage.py migrate but when I add a new field to my model, this error occurs. I also have tried deleting all the migrations and doing the makemigrations and migrate things again but it still does not solve the problem.

Basically I have a model Schedule and I added a Customer field in it which the relationship is customer = models.ForeignKey(Customer)

Here is my model:

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)

Here is the migration:

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')),
            ],
        ),
    ]

Here is the traceback:

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

Upvotes: 2

Views: 5885

Answers (3)

Agustin Gonzalez Ribas
Agustin Gonzalez Ribas

Reputation: 300

In my case, I opened my sqlite database and I manually added that field. This solved the problem for me.

Upvotes: 0

dev
dev

Reputation: 1

You named it twice.

customer = ...(Customer)

Django can't distinguish.

Upvotes: 0

Alasdair
Alasdair

Reputation: 309109

If you deleted all the migrations and re-ran makemigrations, then your migrations and your database will be out of sync.

Your easiest fix is to drop the database and run migrate again. If you've got important data and can't drop the database, then getting the database and migrations back in sync could be tricky. You'll need to create the customer_id column manually, and possibly make other changes as well.

Upvotes: 2

Related Questions