Askew
Askew

Reputation: 101

Django deleted DDBB table and cannot migrate anymore: no such table error

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

Upvotes: 0

Views: 813

Answers (2)

weAreStarsDust
weAreStarsDust

Reputation: 2752

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

Upvotes: 0

Bruno Antonellini
Bruno Antonellini

Reputation: 11

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

Upvotes: 1

Related Questions