Django用户表已删除,无法重新迁移

时间:2019-04-21 03:28:34

标签: django postgresql

出于最好的原因,我删除了我个人Django项目的数据库中的所有表和迁移文件。我以为运行makemigrationsmigrate可以将其备份,但是migration命令失败。

运行python manage.py migrate会导致:

  

django.contrib.auth.models.DoesNotExist:用户匹配查询不存在   存在。

我可以运行服务器。但是,转到管理页面(http://127.0.0.1:8000/admin/)时,出现异常:

  

在处理上述异常期间(“ SessionStore”对象没有   属性“ _session_cache”),则发生另一个异常:

     

上述异常(关系“ django_session”不存在第1行:   ... ession_data”,“ django_session”。“ expire_date”来自“ django_se ... ^”   是以下异常的直接原因:

请求信息显示“ [无法检索当前用户]”。

我能够重新创建超级用户,除主应用程序中的模型外,我拥有所有表:

UNKNOWN_USER_ID = 11    

def get_unknown_user():
        return User.objects.get(id=UNKNOWN_USER_ID).pk

class CommonInfo(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False, verbose_name='Created')
    created_by = models.ForeignKey(User, on_delete=models.PROTECT, default=get_unknown_user,
                                   related_name='%(app_label)s_%(class)s_created', verbose_name='Created By')
    modified = models.DateTimeField(auto_now=True, verbose_name='Modified')
    modified_by = models.ForeignKey(User, on_delete=models.PROTECT, default=get_unknown_user,
                                    related_name='%(app_label)s_%(class)s_modified', verbose_name='Modified By')

    class Meta:
        abstract = True


class CollectionItem(CommonInfo):
    NONE_SELECTED = 0
    DONATED = 1
    PURCHASED = 2
    TRADED = 3
    FOUND = 4
    CREATED = 5

    METHOD_ACQUIRED_CHOICES = (
        (NONE_SELECTED, ''),
        (DONATED, 'Donated'),
        (PURCHASED, 'Purchased'),
        (TRADED, 'Traded'),
        (FOUND, 'Found'),
        (CREATED, 'Created')
    )

    date_acquired = models.DateField(default=date.today, verbose_name='Date Acquired')
    method_acquired = models.IntegerField(choices=METHOD_ACQUIRED_CHOICES, default=NONE_SELECTED,
                                          verbose_name='Method Acquired')
    available_for_trade = models.BooleanField(default=False, verbose_name='Available For Trade')
    tags = TaggableManager(verbose_name='Tags')
    description = models.CharField(max_length=512, blank=True, verbose_name='Description')
    collection = models.ForeignKey('Collection', on_delete=models.PROTECT, verbose_name='Collection ID')

    class Meta:
        abstract = True


class CollectionType(CommonInfo):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name


class Collection(CommonInfo):
    name = models.CharField(max_length=100)
    collection_type = models.ForeignKey('CollectionType', on_delete=models.PROTECT)
    owner = models.ForeignKey(User, on_delete=models.PROTECT, default=get_unknown_user,
                              related_name='%(app_label)s_%(class)s_owned')
    # fieldset = models.ForeignKey('CollectionFieldset', on_delete=models.PROTECT)

    def __str__(self):
        return self.name


class CollectionItemImage(CommonInfo):
    image = models.ImageField(verbose_name='Image')
    collection_item = models.ForeignKey('BottleCap', on_delete=models.PROTECT, verbose_name='Collection Item')
    order_in_collection = models.IntegerField(verbose_name='Order', default=1)


class BottleCap(CollectionItem):
    company = models.CharField(max_length=100, blank=True, verbose_name='Company')
    brand = models.CharField(max_length=100, blank=True, verbose_name='Brand')
    product = models.CharField(max_length=100, blank=True, verbose_name='Product')
    variety = models.CharField(max_length=100, blank=True, verbose_name='Variety')
    color = models.IntegerField(default=None, blank=True, null=True, verbose_name='Color')
    text = models.CharField(max_length=200, blank=True, verbose_name='Text')
    underside = models.CharField(max_length=50, blank=True, verbose_name='Underside')

2 个答案:

答案 0 :(得分:0)

假设您想重新开始,并且对以前的数据不感到困扰。删除db.sqlite3和除 init .py以外的所有迁移之后,创建一个超级用户。不要删除 init .py。

  1. 删除迁移和db.sqlite3

  2. 运行makemigrations

  3. 运行迁移

  4. 创建超级用户

答案 1 :(得分:0)

要在psql中删除整个数据或仅从应用程序删除数据,必须按时间顺序执行此操作:

首先在您的应用文件夹(应用/迁移)中删除迁移对象。

然后:

在psql中删除数据库:

  # DROP DATABASE dbname;

在psql中创建数据库:

  # CREATE DATABASE dbname;

再次进行迁移:

  manage.py make migrations (appname)

然后迁移:

 manage.py migrate (appname)