0

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.
5
  • 1
    When you flushed the DB did you delete all the tables including the migrations table? Commented Aug 3, 2020 at 8:06
  • i ran "manage.py flush" - does that delete all the tables? Commented Aug 3, 2020 at 8:09
  • 1
    It doesn't, you should probably drop and recreate the database to get a fresh one Commented Aug 3, 2020 at 8:11
  • 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. Commented Aug 3, 2020 at 8:14
  • dropping the database and recreating it, then rerunning makemigrations and migrate fixed the issue. Thank you for your help!! Commented Aug 3, 2020 at 8:17

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.