1

我有这个自定义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.

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

重命名主键失败“无法删除表上的约束,因为其他对象依赖于它”

无法删除表用户,因为其他对象依赖于它

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

4

1 回答 1

1

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

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

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

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

python manage.py makemigrations
python manage.py migrate

祝你好运 :)

于 2017-04-27T07:37:08.950 回答