迁移到 POSTGRsql 时出现 Django 错误,无法删除序列 home_userinformation_id_seq,因为表 home_userinformation 的列 id 需要它

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

我的django应用程序迁移成功,但是当我尝试迁移它时,它提示以下错误:

    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\shazi\OneDrive\Desktop\arabic\env\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.InternalError: cannot drop sequence home_userinformation_id_seq because column id of table home_userinformation requires it
HINT:  You can drop column id of table home_userinformation instead.
from django.db import models


class UserInformation(models.Model):
    username = models.CharField(max_length=50)
    level = models.PositiveIntegerField(default=1)
    xp = models.PositiveBigIntegerField(default=0)

    def update_xp(self, amount):
        self.xp += amount
        self.save()  # Save the model instance

    def update_level(self):
        if self.xp > self.level_up_threshold(self.level):
            self.level += 1
            self.save()

    def level_up_threshold(self, level):  # Indent for class method
        return level * 100

    def __str__(self):
        return self.username
    
class Module(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    xp_reward = models.PositiveIntegerField(default=0)  # XP awarded for completing the module

    def __str__(self):
        return self.name
    
class CompletedModule(models.Model):
    user = models.ForeignKey(UserInformation, on_delete=models.CASCADE)
    module = models.ForeignKey(Module, on_delete=models.CASCADE)
    completion_date = models.DateTimeField(auto_now_add=True)  # Records date of completion

    class Meta:
        unique_together = ('user', 'module')  # Ensures a user can't complete a module twice
python django postgresql django-models
1个回答
0
投票

Postgress 不允许您删除一个或多个表正在使用的序列。您可以通过

找到所有使用过的序列

SELECT
    s.relname AS sequence_name,
    n.nspname AS schema_name,
    t.relname AS table_name
FROM
    pg_depend d
JOIN
    pg_class s ON d.objid = s.oid
JOIN
    pg_class t ON d.refobjid = t.oid
JOIN
    pg_namespace n ON t.relnamespace = n.oid
WHERE
    s.relkind = 'S' AND
    d.classid = 'pg_class'::regclass AND
    d.refclassid = 'pg_class'::regclass
ORDER BY
    s.relname, t.relname;

也许您需要更改序列,然后尝试删除它 -更改序列

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