无法在django迁移中创建数据库表

问题描述 投票:0回答:3

我的 django 项目中有 2 个应用程序“app1”、“app2”。

当我跑

python3 manage.pymakemigrations
时,我可以看到

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

但是当我运行

python3 manage.py migrate
时,我收到错误消息

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

“auth_user”是app1.User模型的数据库表,也是AUTH_USER_MODEL所指的。

我尝试单独运行迁移。对于app2来说还好。但是当我运行

python3 manage.py migrate
app1 时,我得到了

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.

我找不到适合我的情况的解决方案,有人可以帮忙吗?

用户型号代码:

类用户(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
django database-migration django-migrations
3个回答
1
投票

为每个应用程序单独使用此命令:

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

0
投票

这是个坏主意,但试试这个。删除所有迁移、缓存文件,并对每个应用程序不完全运行 makemigrations。然后运行迁移


0
投票

我有同样的错误,但因为我尝试在开发过程中修改 AUTH_USER。

在我的例子中,有效的方法是将 django 降级到 2.0,删除所有迁移并不得不删除数据库。

但是它起作用了。

然后,当升级到 4.0 时,必须应用“auth”迁移,但工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.