0

I've deleted a table from the sqlite DDBB because I wanted to make some changes and wasn't able to do those with a simple update of the models in Django. Since then, I am not able to perform migrations again and get a new table for my models, getting the error "django.db.utils.OperationalError: no such table: shop_productitem".

I've tried to follow many hints in other similar threads with no luck. I've tried this and this for example and I cannot solve the issue. My understanding is that if I run makemigrations and then migrate the models the ddbb should update itself with the new specifications, but I'm probably mistaken in this specific case.

Can someone address me where the problem would sit? I fear I have not the necessary knowledge to figure this out of my own, I've been looking for ages.

Please find below the whole error message I'm getting:

    Applying shop.0002_auto_20200218_1958...Traceback (most recent call last):
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: shop_productitem

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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/EconopecoWebApp/.virtualenvs/django2/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/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/migration.py", line 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 133, in alter_field
    super().alter_field(model, old_field, new_field, strict=strict)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 509, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 330, in _alter_fi
eld
    self._remake_table(model, alter_field=(old_field, new_field))
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 260, in _remake_t
able
    disable_constraints=False,
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 95, in alter_db_t
able
    super().alter_db_table(model, old_db_table, new_db_table)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 383, in alter_db_tab
le
    "new_table": self.quote_name(new_db_table),
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 117, in execute
    cursor.execute(sql, params)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/EconopecoWebApp/.virtualenvs/django2/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/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrapp
ers
    return executor(sql, params, many, context)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/EconopecoWebApp/.virtualenvs/django2/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/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: shop_productitem

Thank you so much, Askew

Askew
  • 101
  • 11

2 Answers2

1

Have you tried deleting every migration from the migrations folder (everyone but the init one), and then running makemigrations and migrate?

  • migrate history saved in db too. So just deleting files won't do the deal. – weAreStarsDust Feb 19 '20 at 18:17
  • Hi Bruno, if I do that, the migrations is completed without any error initially, but if I check in my admin and try to open productitem I get OperationalError at /admin/shop/productitem/ no such table: shop_productitem again :( – Askew Feb 19 '20 at 18:24
  • Hi weAreStarDust, could you give me a hint on how to do that? I haven't tried doing that actually and could be the way to go. Many thanks – Askew Feb 19 '20 at 18:25
  • @Askew migrate history stored in `django_migrations` table – weAreStarsDust Feb 19 '20 at 18:36
  • I've never done what weAreStarDust said, but by what i'm seeing right now in Postgres, there's a table labeled **django_migrations** where every migration done is registered. You may try truncating it. – Bruno Antonellini Feb 19 '20 at 18:37
0

If you can afford lose data from db, then just remove old db file from it's folder. delete all migrations from migrate folders. and then run makemigrations and migrate this will create new sqllight DB

Another option if data loss is not an option, you can try recreate deleted table manually with DB Browser for SQLite or with Command Line Shell For SQLite. And then delete them correctly using migrations

For future, if you need to delete table, you should delete or comment it model code and then run makemigrations, migrate and this will delete table correctly from db

weAreStarsDust
  • 2,634
  • 3
  • 10
  • 22
  • Oh yes so that is what I did, I did manage.py dbshell, then drop the tables I wanted to get rid of, then deleted the old migrations and run make migrations and migrate again. No luck whatsoever! – Askew Feb 19 '20 at 18:29
  • @Askew I suggested two scenarios, ask if something is not clear. – weAreStarsDust Feb 19 '20 at 18:51
  • 1
    I recreated the table manually via command line and followed your instructions, and worked like a charm. Thank you so much I was stuck here for ages! Many thanks! :) – Askew Feb 19 '20 at 19:51
  • @Askew Glad to hear, it's good practise to make backup on every manipulation with database, this will save you a lot of time :) – weAreStarsDust Feb 19 '20 at 20:00