简体   繁体   English

如何更改“ id”字段的初始值?

[英]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.

相关问题 在视图中更改初始表单字段值(Django) - Change initial form field value in views (Django) 如何在保存之前更改 Django 表单字段值? - How can I change a Django form field value before saving? 如何使用方法自动更改 django model 字段值? - How can I change django model field value automatically with method? 如何使用初始值递归地将参数输入到此函数中? - How can I recursively input parameters into this function with an initial value? 如何在文本字段中显示初始值? - How to display an initial value in the text field? Django:如何在管理员的更改视图(权限可以查看)中隐藏具有空值的字段 - Django : How can i hide field with empty value in change view ( permission can view ) in admin 如何手动呈现其初始值设置的表单字段? - How can you manually render a form field with its initial value set? 如何使用相关模型中的对象填充ModelForm中Field的初始值? - How would I use the objects from a related Model to populate the initial value of a Field in a ModelForm? 选择特定选项后,如何更改 Django model 字段的值? - How can I change the value of a Django model field when a particular choice is selected? 如何使用 Python 更改或清除 Jira 中分辨率字段的值? - How I can change or clear value for Resolution field in Jira using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM