在Django表中设置AUTOINCREMENT值 [英] Set AUTOINCREMENT value in django table
问题描述
我在mysql中有下表:
I have the following table in mysql:
CREATE TABLE `portal_asset` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`asset_id` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1000000 DEFAULT CHARSET=utf8;
我如何在Django中创建同一个表?到目前为止,我有以下内容,但不确定如何设置AUTO_INCREMENT
值-
How would I create thie same table in django? So far I have the following, but not sure how to set the AUTO_INCREMENT
value --
class PortalAsset(models.Model):
id = models.IntegerField(primary_key=True)
asset_id = models.IntegerField(unique=True)
class Meta:
db_table = u'portal_asset'
如何设置AUTO_INCREMENT
值以1000000开始?等同于:
How can I set the AUTO_INCREMENT
value to start off at 1000000 ? The equivalent of:
alter table portal_asset AUTO_INCREMENT=1000000;
推荐答案
您可以使用 RunSQL
操作以执行必要的SQL:
You can use a RunSQL
operation in your migrations to execute the necessary SQL:
migrations.RunSQL("ALTER TABLE portal_asset AUTO_INCREMENT=1000000;")
如果尚未运行任何迁移,则可以将其添加到第一次迁移中,以确保在设置新值之前不插入任何行.否则,您将不得不在新迁移中添加此操作.您可以使用python manage.py makemigrations --empty <yourappname>
创建一个新的空迁移.
If you haven't run any migrations, you can add this to your first migration to ensure no rows are inserted before the new value is set. Otherwise you'll have to add this operation in a new migration. You can create a new, empty migration using python manage.py makemigrations --empty <yourappname>
.
这篇关于在Django表中设置AUTOINCREMENT值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!