将 primary_key 更改为 Django AbstractBaseUser 模型

Changing primary_key to Django AbstractBaseUser model

我有这个自定义 User 模型,我在其中将 primary_key 字段更改为 email 字段,如下所示:

class User(AbstractBaseUser, PermissionsMixin):
    # Primary Key of my model 
    email = models.EmailField(max_length=254, primary_key=True)   

    username = models.CharField(_('username'),max_length=30,
        unique=True,
        help_text=_('Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.'),
        validators=[
            RegexValidator(
                r'^[\w.ñ@+-]+$',
                _('Enter a valid username. This value may contain only '
                  'letters, numbers ' 'and @/./+/-/_ characters.')
            ),
        ],
        error_messages={
            'unique': _("A user with that username already exists."),
        },
    )

    first_name = models.CharField(max_length=50,blank=True,         null=True,
    )

    last_name=models.CharField(max_length=50, blank=True,            null=True,)

    is_staff = models.BooleanField(
        default=True,
        help_text='Designates whether the user can log into this admin site.')

    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(default=timezone.now)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']
    objects = UserManager()

我还有一个模型叫Match:

class Match(models.Model):

    match_date = models.DateTimeField(default=timezone.now, null=True)

    home_team_players_accept = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        related_name='home_team_players_accept',
        blank=True,)

    away_team_players_accept = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        related_name='away_team_players_accept',
        blank=True,)

    home_team_players_cancel = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        related_name='home_team_players_cancel',
        blank=True,)

    away_team_players_cancel = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        related_name='away_team_players_cancel',
        blank=True,)

    fichaje_players_match = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        related_name='fichaje_players_match',
        blank=True,)

当我执行 python manage.py migrate 时,我得到这个输出:

File "/home/bgarcial/.virtualenvs/fuupbol2/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.InternalError: cannot drop constraint auth_user_pkey on table auth_user because other objects depend on it
DETAIL:  constraint auth_user_groups_user_id_6a12ed8b_fk_auth_user_username on table auth_user_groups depends on index auth_user_pkey
constraint auth_user_user_permissio_user_id_a95ead1b_fk_auth_user_username on table auth_user_user_permissions depends on index auth_user_pkey
constraint games_information_match__user_id_246b2ea3_fk_auth_user_username on table games_information_match_away_team_players_cancel depends on index auth_user_pkey
constraint games_information_match__user_id_9d9f8df1_fk_auth_user_username on table games_information_match_home_team_players_cancel depends on index auth_user_pkey
constraint games_information_match__user_id_79122347_fk_auth_user_username on table games_information_match_fichaje_players_match depends on index auth_user_pkey
constraint games_information_match__user_id_54e7681b_fk_auth_user_username on table games_information_match_away_team_players_accept depends on index auth_user_pkey
constraint games_information_match__user_id_14203632_fk_auth_user_username on table games_information_match_home_team_players_accept depends on index auth_user_pkey
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

我找到了一些关于这个场景案例的参考资料,但我不明白如何解决这个不便

Renaming a primary key fails "cannot drop constraint on table because other objects depend on it"

我什至尝试删除整个数据库,但仍然存在不便。

在您项目的文件夹 migrations 中,有一些文件的名称如下:

0001_initial.py
0002_auto_ ... .py etc.

这些文件是您的数据库迁移历史,由于您刚刚删除了数据库,我假设您不需要迁移历史。

因此,简单的解决方案是清除您的 migrations 文件夹(删除所有 除了 __init__.py 文件),再次删除您的数据库并:

python manage.py makemigrations
python manage.py migrate

祝你好运:)