2

For my basic, rudimentary Django CMS, in my effort to add a toggle feature to publish / unpublish a blog post (I’ve called my app ‘essays’ and the class object inside my models is is_published), I’ve encountered an OperationalError when trying to use the Admin Dashboard to add essay content. I’m expecting to be able to switch a tick box to publish/unpublish but now I can’t even access the Dashboard.

Here is part of the traceback from my Django server:

File "/home/<user>/dev/projects/python/2018-and-2020/<projectdir>/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/<user>/dev/projects/python/2018-and-2020/<projectdir>/venv/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: essays_essayarticle.is_published

The debug traceback reinforces the OperationalError above:

OperationalError at /admin/essays/essayarticle/
no such column: essays_essayarticle.is_published

Request Method: GET
Request URL: http://<DN>.ngrok.io/admin/essays/essayarticle/
Django Version:2.2.11
Exception Type: OperationalError
Exception Value:
no such column: essays_essayarticle.is_published
Exception Location:
/home/<user>/dev/projects/python/2018-and-2020/<projectdir>/venv/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py in execute, line 383
Python Executable:
/home/<user>/dev/projects/python/2018-and-2020/<projectdir>/venv/bin/python
`Fri, 1 May 2020 19:37:31 +0000

Exception Value indicates ‘no such column’ which is a reference to my db. I’m running SQlite3 for test purposes. Prior to this OperationalError, I was troubleshooting issues with a DummyNode and "isinstance django.db.migrations.exceptions.NodeNotFoundError: Migration". The previous solution I arrived at was to delete my migrations in two of my apps. This SO answer is the particular solution that resolved the issue: https://stackoverflow.com/a/56195727/6095646

As per @Laila Buabbas's suggestion, to clarify, I deleted my migrations directory and invoked: python manage.py makemigrations app_name for each of my two apps. So that previous SQLite issue has been resolved. But I can’t figure out this new OperationalError, described above.

Is the issue is with my views.py / models.py (copied below)? I can't narrow it down any more specific than that. Here is part of the class defined in my app’s models.py (with the new potential problem line added at the end):

class EssayArticle(models.Model):
   title = models.CharField(max_length=256)
   web_address = models.CharField(max_length=256)
   web_address_slug = models.SlugField(blank=True, max_length=512)
   content = models.TextField(blank=True)
   is_published = models.BooleanField(default=True)

Here are the relevant lines from the applicable function in my views.py:

def article(request, web_address):
   try:
       article = EssayArticle.objects.get(
           web_address_slug=web_address)  # .filter(is_published=True)
   except EssayArticle.DoesNotExist:
       raise Http404('Article does not exist!')
   context = {
       'article': article,
   }
   return render(request, 'essays/article.html', context)

Commenting in our out the .filter(is_published=True) doesn't stop or change the debug error.

My local dev box is Manjaro Linux with Django v2.2.11. I’m running Python v3.8.2.

Here are some of the resources I have already leveraged:

enoren5
  • 403
  • 5
  • 18

3 Answers3

2

There isn't column is_published in essays_essayarticle table of your db try to add column in db by adding new migration and see the change for a table whether this column is going to be added.

The error isn't in your view rather it is in query.

Engr Umair
  • 130
  • 10
  • When you say 'add a new migration' do you mean `$ python manage.py makemigrations`? I ask because I thought I clarified that I already tried that - - I used makemigrations and migrating which didn't resolve my issue. Is there another way to add a new migration as you say? – enoren5 May 01 '20 at 23:41
1

Its looking like your migration with the same name is already triggered, thats why Django is not able to create new column,

You have to look into the table django_migrations in database find and delete the migration record. You need to compare your migrations with django_migrations in database then only you will find the issue/error you are getting

Once you delete the migration from django_migrations in database,Run the migration

./manage.py migrate

then might your problem get solved

Zaheer Jan
  • 233
  • 4
  • 13
  • Yes, deleting and building a migration from scratch would fix the issue. Even though it is a test case, I still need to preserve the data being stored. I can't build a db from scratch. How do I add/delete the right table/row/column/entry? Here is what it looks like in the tool that I am using: https://imgur.com/NeIxvx8 – enoren5 May 08 '20 at 21:11
  • This is the tool I'm using pictured in that image above: https://sqlitebrowser.org/ Is there another way to achieve the same effect but in a unix shell via CLI? – enoren5 May 08 '20 at 21:17
  • https://www.sqlitetutorial.net/sqlite-commands/ this might help you on playing with sqlite database. – Zaheer Jan May 08 '20 at 22:09
0

I'm making the assumption that you deleted the migrations folder, if so when you makemigrations and migrate write the name of you app at the end

example

python manage.py makemigrations app_name
  • Yes, I deleted the migrations folder for my two web apps and invoked `makemigrations` for both specific apps. The OperationalError persists. I updated my question so as to avoid other people having to make the same assumption you had to make. – enoren5 May 01 '20 at 21:55