azteker
azteker

Reputation: 61

Can not create db table in django migration

I have 2 apps "app1", "app2" in my django project.

When I ran python3 manage.pymakemigrations, I can see

...
Migrations for 'app1':
...
Migrations for 'app2':
...

But when I ran python3 manage.py migrate, I got error saying

django.db.utils.ProgrammingError: relation "auth_user" does not exist

"auth_user" is db table for app1.User model, also is what AUTH_USER_MODEL refers to.

I tried to run migrate separately. It is fine for app2. But when I run python3 manage.py migrate app1, I got

No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

I cannot find a solution for my case, anyone can help?

The user model code:

class User(AbstractBaseUser, Base):

USER_TYPE_CHOICES = (
    (1, 'supervisor'), 
    (2, 'admin'),
    (4, 'support'),
)

email = models.EmailField(primary_key=True, max_length=256)
name = models.CharField(max_length=45, null=True)
password = models.CharField(max_length=256, null=True)
last_login = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=True)
user_type = models.PositiveSmallIntegerField(
    choices=USER_TYPE_CHOICES,
    default=2
)

USERNAME_FIELD = 'email'
objects = UserManager()

class Meta:
    db_table = 'auth_user'
    app_label = 'admin_portal'

def __str__(self):
    return str(self.email)

@property
def is_admin(self):
    return self.user_type == 1

@property
def is_active(self):
    return self.active

Upvotes: 0

Views: 932

Answers (3)

Mutox
Mutox

Reputation: 1

I had the same error but because I tried modifying the the AUTH_USER during development.

In my case what worked was downgrading django to 2.0, deleted all migrations and had to drop the database.

But it worked.

Then when upgrading to 4.0 had to apply "auth" migration but worked fine.

Upvotes: 0

shiva gangula
shiva gangula

Reputation: 45

Its bad idea but try this. Delete all migration, cache files nd do run makemigrations to each app not entirely.then run the migrate

Upvotes: 0

sNezamHa
sNezamHa

Reputation: 144

Use this command separately for each app:

python3 manage.py makemigrations app1
python3 manage.py makemigrations app2

Upvotes: 1

Related Questions