[英]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()
的类型:
uuid = models.UUIDField(default=uuid.uuid4, primary=True)
(default will set the field for existing rows)uuid = models.UUIDField(default=uuid.uuid4, primary=True)
(默认将为现有行设置字段)id
fieldid
字段uuid
field to id
uuid
字段重命名为id
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.