简体   繁体   English

在 Django (Django 3.0) 中将现有 id 转换为 uuid 后修复错误“格式错误的十六进制 UUID 字符串”

[英]Fixing error “Badly formed hexadecimal UUID string” after converting existing id to uuid in Django (Django 3.0)

I created a table with an initial IntegerField primary key and later changed the id to a UUIDField.我创建了一个带有初始 IntegerField 主键的表,后来将 id 更改为 UUIDField。 Now this raises a "Badly formed hexadecimal UUID string" , I guess because a number such as "1" isn't a valid UUID value.现在这引发了“格式错误的十六进制 UUID 字符串” ,我猜是因为诸如“1”之类的数字不是有效的 UUID 值。 Does anyone know a concise way to fix this in code when updating the models.py file for the django app?在更新 django 应用程序的 models.py 文件时,有谁知道在代码中解决此问题的简洁方法?

Before everything, double check that you don't have any foreign key with Cascade uppon this model.在一切之前,仔细检查你在这个 model 上没有任何外键。 Then, instead of changing the type of id = models.IntegerField() :然后,不要更改id = models.IntegerField()的类型:

  • create a new field uuid = models.UUIDField(default=uuid.uuid4, primary=True) (default will set the field for existing rows)创建一个新字段uuid = models.UUIDField(default=uuid.uuid4, primary=True) (默认将为现有行设置字段)
  • make migrations进行迁移
  • remove the id field删除id字段
  • make migrations进行迁移
  • rename the uuid field to iduuid字段重命名为id
  • make migrations进行迁移
  • migrate迁移

However, you should know that removing the id field, and / or naming the uuid is not mandatory, and it's often a good idea to keep both.但是,您应该知道删除id字段和/或命名uuid不是强制性的,保留两者通常是个好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django装置中格式错误的十六进制uuid字符串错误 - Badly Formed hexadecimal uuid string error in Django fixture Django UUIDField 显示“格式错误的十六进制 UUID 字符串”错误? - Django UUIDField shows 'badly formed hexadecimal UUID string' error? Django:如何修复 UUID ValueError('格式错误的十六进制 UUID 字符串')? - Django: How to fix UUID ValueError('badly formed hexadecimal UUID string')? ValueError格式错误的十六进制UUID字符串Django ListView - ValueError badly formed hexadecimal UUID string django ListView Django UUIDField modelfield导致Django管理员出错:格式错误的十六进制UUID字符串 - Django UUIDField modelfield causes error in Django admin: badly formed hexadecimal UUID string FastAPI - 格式错误的十六进制 UUID 字符串 - FastAPI - badly formed hexadecimal UUID string 带有Adafruit库的格式错误的十六进制UUID字符串 - badly formed hexadecimal UUID string with Adafruit library 在处理上述异常(格式错误的十六进制 UUID 字符串)时,又发生了一个异常: - During handling of the above exception (badly formed hexadecimal UUID string), another exception occurred: UUID使用str(value)发送给客户端,但是当接收回格式错误的十六进制字符串时,发送给ib服务器 - UUID send to client with str(value) but ib server when receive back badly formed hexadecimal string Django:在 django 模板引擎中将 uuid 值转换为字符串 - Django: Converting uuid value to a string in django template engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM