[英]How can I change the initial value of `id` field?
I have a User
model, which generated the bellow table: 我有一个
User
模型,它生成了下面的表格:
in it the id is increase from 1
. 其中id是从
1
增加。
I have used my system in a spell, now I want to change the id
of some user instance and start at 10000
. 我已经在一个咒语中使用了我的系统,现在我想更改某个用户实例的
id
并从10000
开始。
in the database I can not change, it will report error: 在数据库中我无法更改,它将报告错误:
Cannot delete or update a parent row: a foreign key constraint fails (`qy`.`usermanage_user_groups`, CONSTRAINT `usermana_user_id_53f44f10_fk_qy_adm` FOREIGN KEY (`user_id`) REFERENCES `usermanage_user` (`id`))
Is it possible to change the id
? 是否可以更改
id
? if there is some related factors, how can I solved it one by one, such as usermanage_user_groups
? 如果有相关因素,我该如何一一解决,例如
usermanage_user_groups
?
The id
field is an automatic primary key field added by Django since you did not provide the primary_key=True
property. id
字段是Django添加的自动主键字段,因为您没有提供primary_key=True
属性。
NOTE 注意
Now, as the comments says you probably should not have to change the behavior of that field since it guarantee uniqueness throughout your data. 现在,正如评论所说,您可能不必更改该字段的行为,因为它可以保证整个数据的唯一性。 See linked documentation for more information.
有关更多信息,请参见链接的文档。
Considering you understood that by now, there is two possiblities for you in my opnion. 考虑到您已经了解到,在我看来,您有两种可能。
Preferred solution I think 我认为首选的解决方案
1) Add a custom AutoField and start it at 10000
and it will then update accordingly. 1)添加一个自定义的AutoField并从
10000
开始,然后将进行相应的更新。
Hack version 哈克版
2) Add a user with an id
of 9999 in your database. 2)在您的数据库中添加一个
id
为9999的用户。 This will result in auto-incrementing your id
field according to this new value. 这将导致根据此新值自动增加您的
id
字段。 It won't fill the values in between. 它不会填充介于两者之间的值。
You can setup sequence start from 10000 in your database. 您可以在数据库中从10000开始设置序列。 Also may trying manually add user with id 9999 through db manager.
也可以尝试通过数据库管理器手动添加ID为9999的用户。
I sort of understand what you want to achieve, but I'm not sure if you want to retain the existing user data across your tables including user, usermanage_user, usermanage_user_groups; 我有点理解要实现的目标,但是不确定是否要在表中保留现有的用户数据,包括user,usermanage_user,usermanage_user_groups;
Meaning, Do you simply want to replace user_id from [1, 2, 3, 4, ......25] to [10000, 10001, 10002, 10004, ...... 10025] for the existing users? 意思是, 您是否只是要将现有用户的user_id从[1,2,3,4,... 25]替换为[10000,10001,10002,10004,... 10025]?
1] If yes, then manipulating tables in DB to safely update ID in all the referred table is one way. 1]如果是,那么操纵数据库中的表以安全更新所有引用表中的ID是一种方法。 I can give a better solution here, if you share your schema structures / django models.
如果您共享架构结构/ django模型,那么我可以在此处提供更好的解决方案。
2] If no, you can simply set AUTO_INCREMENT to 10000, so that the following newly added users begin with 10000+ 2]如果否,则只需将AUTO_INCREMENT设置为10000,以便以下新添加的用户以10000+开始
`ALTER TABLE User AUTO_INCREMENT = 10000;`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.