0

添加新字段后迁移错误使用 django-pgcrypto-fields

我的模型(第一次迁移没问题)

class Dkm(models.Model):
    name = fields.TextPGPSymmetricKeyField()
    value = fields.IntegerPGPSymmetricKeyField(default=0)

我更新模型并再次迁移:

class Dkm(models.Model):
    name = fields.TextPGPSymmetricKeyField()
    value = fields.IntegerPGPSymmetricKeyField(default=0)
    value3 = fields.IntegerPGPSymmetricKeyField(default=0)

发生错误

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\core\management\base.py", line 364, in execute
    cursor.execute(sql, params)
  File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\backends\utils.py", line 99, in execute
    return super().execute(sql, params)
  File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "value3" is of type bytea but default expression is of type integer
HINT:  You will need to rewrite or cast the expression.

如果设置 null=True 则不会发生错误

4

1 回答 1

0

错误

列“value3”是 bytea 类型,但默认表达式是整数类型

指的是default=0您的字段定义中的内容。

它仅在您第二次使用添加的字段迁移时出现的原因value3 是因为它实际上是在寻找已经存在的记录的默认值。而您第一次迁移时不存在任何记录。

于 2021-10-01T08:30:20.253 回答