0
votes

I renamed some of my models and tried to apply the migrations, but Django didn't detect that they were renamed. Therefore I deleted the content of manage.py, flushed the db and then ran manage.py makemigrations and manage.py migrate. Now I get the following error when I try to access one of my models via admin console or via any queries:

ProgrammingError at /admin/restapi/appuser/
relation "restapi_appuser" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "restapi_appuser"

When I ran manage.py makemigrations the output included (truncated):

Migrations for 'restapi':
  restapi/migrations/0001_initial.py
    - Create model AppUser

I tried to run python3 manage.py sqlmigrate restapi 0001_initial with the following output (truncated):

BEGIN;
--
-- Create model AppUser
--
CREATE TABLE "restapi_appuser" ("id" serial NOT NULL PRIMARY KEY, "username" varchar(30) NOT NULL UNIQUE, "email" varchar(50) NOT NULL UNIQUE, "password" varchar(50) NOT NULL, "join_date" timestamp with time zone NOT NULL);

It seems to me like the model should be in the database, but I am wondering why I am getting this error

edit:

here the output of manage.py migrate for completeness:

> python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, restapi, sessions
Running migrations:
  No migrations to apply.
1
When you flushed the DB did you delete all the tables including the migrations table?Iain Shelvington
i ran "manage.py flush" - does that delete all the tables?Nik
It doesn't, you should probably drop and recreate the database to get a fresh oneIain Shelvington
You can try python manage.py migrate restapi zero to undo the first migration, then retry python manage.py migrate. If the zero migration fails because the table doesn't actually exist, try it with --fake.AKX
dropping the database and recreating it, then rerunning makemigrations and migrate fixed the issue. Thank you for your help!!Nik

1 Answers

0
votes

sqlmigrate only shows you the SQL for a migration.

You'll need to run migrate for Django to actually apply pending migrations into your database after you've makemigrationsed.