gpt4 book ai didi

Django 1.9 - 更新时出现 IntegrityError

转载 作者:行者123 更新时间:2023-12-02 04:38:30 28 4
gpt4 key购买 nike

class Configuration(models.Model):
configuration_key = models.CharField(max_length=100, unique=True)
configuration_value = models.TextField()
configuration_description = models.TextField("a brief description of this setting", blank=True)

以上是我的模型。我能够使用管理员添加配置。我没有在管理员上配置任何自定义项。这是一个非常基本的

admin.site.register(Configuration)

当我使用 admin 更新现有配置时,它会抛出 IntegrityError

duplicate key value violates unique constraint "config_configuration_configuration_key_key"
DETAIL: Key (configuration_key)=(SAMPLE_CONFIGURATION) already exists.

我的问题:管理员难道不应该知道现有配置已被修改并相应地处理吗?我错过了什么吗?要清楚 - SAMPLE_CONFIGURATION - 表中只有一个这样的行具有该 configuration_key。我正在尝试使用管理员编辑该行的 configuration_value,它会抛出 Integrityerror。

最佳答案

你的代码没问题,一定没有错误。将 Django 更新到当前版本并重试。

同时尝试隔离问题。使用测试重现问题。如果测试通过,则问题不在您的代码中。

仔细检查数据库中的内容,尝试使用 SQL 直接更新行。

此测试更新 configuration_value 没有任何完整性问题。

class ConfigurationModelTest(TestCase):
def test_uniq_issue(self):
config = Configuration.objects.create(configuration_key='SAMPLE_CONFIGURATION',
configuration_value='value')
config.save()

config.configuration_value = 'updated_value'
config.save()

关于Django 1.9 - 更新时出现 IntegrityError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39717619/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com